-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy path25-pathInTree.cpp
More file actions
69 lines (63 loc) · 1.5 KB
/
25-pathInTree.cpp
File metadata and controls
69 lines (63 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
vector<int> path;
typedef struct tNode{
int data;
struct tNode *lchild;
struct tNode *rchild;
}treeNode;
//按照先序遍历的方式创建二叉树
treeNode *createTree()
{
int key;
treeNode *p=NULL;
scanf("%d",&key);
if(key!=-1)
{
p=(treeNode *)malloc(sizeof(treeNode));
p->data=key;
p->lchild=createTree();
p->rchild=createTree();
}
else
return NULL;
return p;
}
void findPath_detail(treeNode *root,int expectedSum,vector<int>& path,int currentSum)
{
currentSum+=root->data;
path.push_back(root->data);
bool isLeaf=root->lchild==NULL &&root->rchild==NULL;
if(currentSum==expectedSum &&isLeaf)
{
printf("A path is found: ");
vector<int>::iterator iter=path.begin();
for(;iter!=path.end();++iter)
printf("%d\t",*iter);
printf("\n");
}
if(root->lchild)
findPath_detail(root->lchild,expectedSum,path,currentSum);
if(root->rchild)
findPath_detail(root->rchild,expectedSum,path,currentSum);
path.pop_back();
}
void findPath(treeNode *root,int expectedSum)
{
if(!root)
return;
std::vector<int> path;
int currentSum=0;
findPath_detail(root,expectedSum,path,currentSum);
}
int main()
{
int expectedSum;
treeNode *T=createTree();
printf("input expectedSum:\n");
scanf("%d",&expectedSum);
findPath(T,expectedSum);
return 0;
}