-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path545.cpp
More file actions
65 lines (60 loc) · 1.16 KB
/
545.cpp
File metadata and controls
65 lines (60 loc) · 1.16 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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
vector<int> res;
void left(TreeNode* root){
if(!root)
return;
if(root->left){
res.push_back(root->val);
left(root->left);
}
else if(root->right){
res.push_back(root->val);
left(root->right);
}
return;
}
void leaf(TreeNode* root){
if(!root)
return;
leaf(root->left);
if(!root->left && !root->right)
res.push_back(root->val);
leaf(root->right);
return;
}
void right(TreeNode* root){
if(!root)
return;
if(root->right){
right(root->right);
res.push_back(root->val);
}
else if(root->left){
right(root->left);
res.push_back(root->val);
}
return;
}
void boundary(TreeNode* root){
if(!root)
return;
res.push_back(root->val);
left(root->left);
leaf(root->left);
leaf(root->right);
right(root->right);
return;
}
vector<int> Solution::solve(TreeNode* A) {
res.clear();
boundary(A);
return res;
}