zoukankan      html  css  js  c++  java
  • 剑指OFFER之二叉树中和为某一值的路径

    题目描述:

    输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。

    输入:

    每个测试案例包括n+1行:

    第一行为2个整数n,k(1<=n<=10000),n表示结点的个数,k表示要求的路径和,结点编号从1到n。                                                                                                       

    接下来有n行。这n行中每行为3个整数vi,leftnode,rightnode,vi表示第i个结点的值,leftnode表示第i个结点的左孩子结点编号,rightnode表示第i个结点的右孩子结点编号,若无结点值为-1。编号为1的结点为根结点。

    输出:

    对应每个测试案例,先输出“result:”占一行,接下来按字典顺序输出满足条件的所有路径,这些路径由结点编号组成,输出格式参照输出样例。

    样例输入:
    5 22
    10 2 3
    5 4 5
    12 -1 -1
    4 -1 -1
    7 -1 -1
    1 5
    1 -1 -1
    
    样例输出:
    result:
    A path is found: 1 2 5
    A path is found: 1 3
    result:

    注意:题目要求要按照字典序进行输出,做以下处理:选取值小的结点作为leftnode,值较大的结点作为rightnode.

    Code:
    #include <cstdio>
    #include <vector>
    #include <set>
     
    using namespace std;
     
    struct BinaryTreeNode{
        int data;
        int lchild;
        int rchild;
    };void findPath(BinaryTreeNode Tree[],int root,int expectedSum,vector<int>& path,int currentSum){
        currentSum+=Tree[root].data;
        path.push_back(root);
        bool isLeaf=Tree[root].lchild==-1&&Tree[root].rchild==-1;
        if(currentSum==expectedSum&&isLeaf){
            printf("A path is found: ");
            vector<int>::iterator iter=path.begin();
            bool isFirst=true;
            for( ;iter!=path.end();++iter){
                if(isFirst){
                    printf("%d",*iter);
                    isFirst=false;
                }
                else
                    printf(" %d",*iter);
            }
            printf("
    ");
        }
        if(Tree[root].lchild!=-1){
            findPath(Tree,Tree[root].lchild,expectedSum,path,currentSum);
        }
        if(Tree[root].rchild!=-1){
            findPath(Tree,Tree[root].rchild,expectedSum,path,currentSum);
        }
        path.pop_back();
    }
     
    void startFind(BinaryTreeNode Tree[],int root,int expectedSum){
        if(root==-1)
            return;
        vector<int> path;
        int currentSum=0;
        findPath(Tree,root,expectedSum,path,currentSum);
    }
     
    int maxNum(int a,int b){
        return a>b?a:b;
    }
     
    int minNum(int a,int b){
        return a<b?a:b;
    }
     
    int main()
    {
        int n,k;
        const int nodeNum=10010;
        BinaryTreeNode Tree[nodeNum];
        while(scanf("%d%d",&n,&k)!=EOF){
            int vi,leftNode,rightNode;
            for(int cnt=1;cnt<=n;++cnt){
                scanf("%d%d%d",&vi,&leftNode,&rightNode);
                Tree[cnt].data=vi;
                Tree[cnt].lchild=minNum(leftNode,rightNode);
                Tree[cnt].rchild=maxNum(leftNode,rightNode);
            }
            printf("result:
    ");
            startFind(Tree,1,k);
        }
        return 0;
    }
     
    /**************************************************************
        Problem: 1368
        User: lcyvino
        Language: C++
        Result: Accepted
        Time:40 ms
        Memory:1568 kb
    ****************************************************************/


  • 相关阅读:
    RuntimeError: An attempt has been made to start a new process before the current
    Expected object of backend CPU but got backend CUDA for argument #2 'weight'
    RuntimeError: multi-target not supported at
    模型load文件时报AttributeError: Can't get attribute 'Cours' on <module '__main__' from 错误
    ArrayList扩容源码分析
    HashMap源码分析(put/get)
    索引优化策略有哪些
    Mysql什么是回表查询和覆盖索引
    MyISAM与InnoDB区别
    使用ReentrantLock实现阻塞队列与交替打印
  • 原文地址:https://www.cnblogs.com/Murcielago/p/4167156.html
Copyright © 2011-2022 走看看