• 【编程题目】在二元树中找出和为某一值的所有路径(树)


    4.在二元树中找出和为某一值的所有路径(树)
    题目:输入一个整数和一棵二元树。
    从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
    打印出和与输入整数相等的所有路径。
    例如 输入整数 22 和如下二元树
    10
    /
    5 12
    /
    4 7
    则打印出两条路径:10, 12 和 10, 5, 7。

    二元树节点的数据结构定义为:
    struct BinaryTreeNode // a node in the binary tree
    {
    int m_nValue; // value of node
    BinaryTreeNode *m_pLeft; // left child of node
    BinaryTreeNode *m_pRight; // right child of node
    };

    思路:

    直接想到了深度优先搜索,写了一个上午的代码

    /*
    4.在二元树中找出和为某一值的所有路径(树)
    题目:输入一个整数和一棵二元树。
    从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
    打印出和与输入整数相等的所有路径。
    例如  输入整数 22 和如下二元树
        10
       /  
      5   12   
    /  
    4  7
    则打印出两条路径:10, 12 和 10, 5, 7。
    
    二元树节点的数据结构定义为:
    struct BinaryTreeNode // a node in the binary tree
    {
    int m_nValue; // value of node
    BinaryTreeNode *m_pLeft; // left child of node
    BinaryTreeNode *m_pRight;  // right child of node
    };
    */
    #include <iostream>
    #include <vector>
    using namespace std;
    
    typedef struct BinaryTreeNode // a node in the binary tree
    {
    int m_nValue; // value of node
    BinaryTreeNode *m_pLeft; // left child of node
    BinaryTreeNode *m_pRight;  // right child of node
    }BinaryTreeNode;
    
    int createBinaryTree(BinaryTreeNode * &T) //创建二叉树
    {
        int data;
        if(scanf("%d",&data) != 0 && data != 0) //输入0 表示null
        {
            T = new BinaryTreeNode;
            T->m_nValue = data;
            T->m_pLeft = NULL;
            T->m_pRight = NULL;
            createBinaryTree(T->m_pLeft);
            createBinaryTree(T->m_pRight);
        }
        return 1;
    }
    
    int DFSFindSum(BinaryTreeNode * T, int sum)
    {
        int ansnum = 0;
        int  ans[20];  //最多20层
        int k = 1;
        vector<BinaryTreeNode *>  S[20];  //最多20层
        S[k - 1].push_back(T);
        while(k >= 1)
        {
            while(!S[k - 1].empty())
            {
                BinaryTreeNode * x = S[k-1].back();
                S[k-1].pop_back();
                ans[k - 1] = x->m_nValue; 
                if(x->m_pLeft != NULL || x->m_pRight != NULL)
                {
                    k++;
                    if(x->m_pLeft != NULL)
                    {
                        S[k-1].push_back(x->m_pLeft);
                    }
                    if(x->m_pRight != NULL)
                    {
                        S[k-1].push_back(x->m_pRight);
                    }
                }
                else
                {
                    int total = 0;
                    for(int i = 0; i < k; i++)
                    {
                        total += ans[i];
                    }
                    if(total == sum)
                    {
                        ansnum++;
                        for(int i = 0; i < k; i++)
                        {
                            printf("%d ", ans[i]);
                        }
                        printf("
    ");
                    }
                }
            }
            if(k >= 1)
            {
                k--;
            }
        }
        if(ansnum == 0)
        {
            printf("no path
    ");
        }
        return 0;
    }
    
    int main()
    {
        BinaryTreeNode * T = NULL;
        createBinaryTree(T);
        DFSFindSum(T, 22);
        return 1;
    }

    后来在网上看了别人写的代码, 发现自己太重视非递归了,其实用递归方便很多

    来源:http://blog.csdn.net/yake25/article/details/7375532

    #include <iostream>
    #include <vector>
    using namespace std;
    
    struct BinaryTreeNode // a node in the binary tree
    {
        int m_nValue; // value of node
        BinaryTreeNode *m_pLeft; // left child of node
        BinaryTreeNode *m_pRight; // right child of node
    };
    
    vector<BinaryTreeNode*> pathVec;
    
    void creatTree(BinaryTreeNode *&root, int *a, int i, int len)
    {
        if(i >= len)
            return;
        root = new BinaryTreeNode;
        root->m_nValue = a[i];
        root->m_pLeft = NULL;
        root->m_pRight = NULL;
        creatTree(root->m_pLeft, a, 2*i + 1, len);
        creatTree(root->m_pRight, a, 2*i + 2, len);
    }
    
    void preorder(BinaryTreeNode* &root)
    {
        if(!root)
            return;
        cout << root->m_nValue << " ";
        preorder(root->m_pLeft);
        preorder(root->m_pRight);    
    }
    
    void printPath(vector<BinaryTreeNode*>& pathVec)
    {
        for(vector<BinaryTreeNode*>::iterator it = pathVec.begin(); it != pathVec.end(); it++)
            cout << (*it)->m_nValue << " ";
        cout << endl;
    }
    
    void pathTree(BinaryTreeNode* &root, int val)
    {
    //输出二叉树中路径等于val的所有路径
        static int sum = 0;
        sum += root->m_nValue;
        pathVec.push_back(root);
        if(sum == val && root->m_pLeft == NULL && root->m_pRight == NULL)
            printPath(pathVec);
        if(root->m_pLeft)
            pathTree(root->m_pLeft, val);
        if(root->m_pRight)
            pathTree(root->m_pRight, val);
        sum -= root->m_nValue;
        pathVec.pop_back();
    }
    
    int main()
    {
        BinaryTreeNode *root = 0;
        int a[] = {10, 5, 12, 7, 8};
        int len = sizeof a / sizeof a[0];
        creatTree(root, a, 0, len);
    //    preorder(root);
        pathTree(root, 22);
    }
  • 相关阅读:
    Vuex的使用
    vue的props属性,vue的插槽
    ES6 Promise对象
    ES6 Map对象以及Set对象
    函数作用域以及块级作用域
    组件之间的传值-$refs&$parent
    Vue中父子组件的传值
    v-on 以及v-model的修饰符以及vue的常用指令
    时间线
    readline和xreadline的区别
  • 原文地址:https://www.cnblogs.com/dplearning/p/3890080.html
Copyright © 2020-2023  润新知