剑指Offer:面试题25

题目描述:
输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
九度OJ地址:http://ac.jobdu.com/problem.php?pid=1368

自己写的代码,自己运行没问题,提交总是不通过 = =

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<stdlib.h>
 4 #include<vector>
 5 #include<memory.h>
 6 using namespace std;
 7 
 8 #define MAX 100
 9 struct BinaryTreeNode{
10     int index;
11     int value;
12     int lchild;
13     int rchild;
14 };
15 
16 BinaryTreeNode p[MAX];
17 vector<BinaryTreeNode> path;
18 
19 
20 void FindPath(BinaryTreeNode* p,int exceptedNum,vector<BinaryTreeNode> path,int currentNum){
21     currentNum=currentNum+(p->value);
22     path.push_back(*p);
23     bool IsLeaf=(p->lchild==-1)&&(p->rchild==-1);
24 
25     if(currentNum==exceptedNum&&IsLeaf)
26     {
27         printf("A path is found: ");
28         vector<BinaryTreeNode>::iterator iter=path.begin();
29         for(;iter!=path.end();++iter)
30         {
31             printf("%d ",iter->index);
32         }
33         printf("
");
34     }
35     if(p->lchild!=-1)
36         FindPath(p+(p->lchild-p->index),exceptedNum,path,currentNum);
37     if(p->rchild!=-1)
38         FindPath(p+(p->rchild-p->index),exceptedNum,path,currentNum);
39     currentNum=currentNum-(p->value);
40     path.pop_back();
41 
42 
43 }
44 void FindPath(BinaryTreeNode* p,int exceptedNum){
45     if(p==NULL)
46        return;
47     int currentNum=0;
48     FindPath(p,exceptedNum,path,currentNum);
49 
50 }
51 int main()
52 {
53     int n,k;
54     int v,l,r;
55     while(scanf("%d %d",&n,&k)==2)
56     {
57         memset(p,-1,sizeof(p));
58         for(int i=0;i<n;i++)
59         {
60             scanf("%d %d %d",&v,&l,&r);
61             p[i].index=i+1;
62             p[i].value=v;
63             p[i].lchild=min(l,r);
64             p[i].rchild=max(l,r);
65         }
66         printf("result:
");
67         FindPath(p,k);
68 
69 
70     }
71     return 0;
72 }

找了大神代码完美运行:

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<vector>
#include<memory.h>
using namespace std;

#define MAX 100
struct BinaryTreeNode{
    int index;
    int value;
    int lchild;
    int rchild;
};

BinaryTreeNode p[MAX];
vector<BinaryTreeNode> path;


void FindPath(BinaryTreeNode* p,int exceptedNum,vector<BinaryTreeNode> path,int currentNum){
    currentNum=currentNum+(p->value);
    path.push_back(*p);
    bool IsLeaf=(p->lchild==-1)&&(p->rchild==-1);

    if(currentNum==exceptedNum&&IsLeaf)
    {
        printf("A path is found: ");
        vector<BinaryTreeNode>::iterator iter=path.begin();
        for(;iter!=path.end();++iter)
        {
            printf("%d ",iter->index);
        }
        printf("
");
    }
    if(p->lchild!=-1)
        FindPath(p+(p->lchild-p->index),exceptedNum,path,currentNum);
    if(p->rchild!=-1)
        FindPath(p+(p->rchild-p->index),exceptedNum,path,currentNum);
    currentNum=currentNum-(p->value);
    path.pop_back();


}
void FindPath(BinaryTreeNode* p,int exceptedNum){
    if(p==NULL)
       return;
    int currentNum=0;
    FindPath(p,exceptedNum,path,currentNum);

}
int main()
{
    int n,k;
    int v,l,r;
    while(scanf("%d %d",&n,&k)==2)
    {
        memset(p,-1,sizeof(p));
        for(int i=0;i<n;i++)
        {
            scanf("%d %d %d",&v,&l,&r);
            p[i].index=i+1;
            p[i].value=v;
            p[i].lchild=min(l,r);
            p[i].rchild=max(l,r);
        }
        printf("result:
");
        FindPath(p,k);


    }
    return 0;
}

慢慢学习吧

原文地址:https://www.cnblogs.com/sunrunzhi/p/3949768.html