FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tree-notes/26rootToNodePath.cpp at main · coder-writes/tree-notes · GitHub
coder-writes
/
tree-notes
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tree-notes
/
26rootToNodePath.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
66 lines (53 loc) · 1.35 KB
Breadcrumbs
tree-notes
/
26rootToNodePath.cpp
Copy path
File metadata and controls
66 lines (53 loc) · 1.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
<
bits/stdc++.h
>
using
namespace
std
;
struct
TreeNode
{
int
data;
struct
TreeNode
* left;
struct
TreeNode
* right;
TreeNode
(
int
val)
{
data = val;
left =
NULL
;
right =
NULL
;
}
};
bool
helper
(TreeNode* root,
int
A,vector<
int
> &arr){
if
(root==
NULL
){
return
false
;
}
arr.
push_back
(root->
data
);
if
(root->
data
==A){
return
true
;
}
if
(
helper
(root->
left
,A,arr) ||
helper
(root->
right
,A,arr))
return
true
;
arr.
pop_back
();
return
false
;
}
vector<
int
>
rootToNodePath
(TreeNode* root,
int
A){
vector<
int
>arr;
if
(root==
NULL
){
return
arr;
}
helper
(root,A,arr);
return
arr;
}
int
main
(){
struct
TreeNode
* root =
new
TreeNode
(
2
);
root->
left
=
new
TreeNode
(
5
);
root->
left
->
left
=
new
TreeNode
(
4
);
root->
left
->
left
->
left
=
new
TreeNode
(
3
);
root->
left
->
left
->
right
=
new
TreeNode
(
2
);
root->
left
->
right
=
new
TreeNode
(
5
);
root->
right
=
new
TreeNode
(
7
);
root->
right
->
left
=
new
TreeNode
(
9
);
root->
right
->
left
->
right
=
new
TreeNode
(
2
);
root->
right
->
left
->
right
->
right
=
new
TreeNode
(
4
);
root->
right
->
left
->
right
->
left
=
new
TreeNode
(
1
);
root->
right
->
left
->
left
=
new
TreeNode
(
3
);
root->
right
->
right
=
new
TreeNode
(
7
);
for
(
auto
&val:
rootToNodePath
(root,
1
)){
cout<<val<<
"
"
;
}
}
Back
|
FazBrowse Home
|
New Git URL