-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_sum_tree.c
More file actions
executable file
·29 lines (27 loc) · 876 Bytes
/
path_sum_tree.c
File metadata and controls
executable file
·29 lines (27 loc) · 876 Bytes
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
#include<iostream>
using namespaces std;
struct node{
int value;
struct node* pleft;
struct node* pright;
};
int find_path(struct node* root, int sum, vector<int>& path, int cur){
cur += root->value;
path.push_back(root->value);
bool is_leaf = root->pleft == NULL && root->pright == NULL;
if(cur == sum && is_leaf){
//print the node in this path
}
if(root->pleft != NULL)
find_path(root->pleft , sum,path,cur);
if(root->pright != NULL)
find_path(root->pright,sum,path,cur);
//when finish to pop the leaf node when this path is not the sum path
path.pop_back()
}
int find_sum_path(struct node* root, int sum){
if(root == NULL)
return -1;
std:vector<int> path;
return find_path(root,sum,path,0);
}