-
Notifications
You must be signed in to change notification settings - Fork 3
/
545.cpp
55 lines (54 loc) · 1.73 KB
/
545.cpp
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> lb;
vector<int> rb;
vector<int> children;
void dfs(TreeNode* node, bool isLeftBound, bool isRightBound) {
if (node == nullptr) return;
if (node->left == nullptr && node->right == nullptr) {
children.push_back(node->val);
return;
}
if (isLeftBound) {
lb.push_back(node->val);
if (node->left != nullptr) {
dfs(node->left, isLeftBound, false);
dfs(node->right, false, false);
}
else dfs(node->right, true, false);
}
else if (isRightBound) {
rb.push_back(node->val);
if (node->right != nullptr) {
dfs(node->left, false, false);
dfs(node->right, false, isRightBound);
}
else dfs(node->left, false, true);
}
else {
dfs(node->left, false, false);
dfs(node->right, false, false);
}
}
vector<int> boundaryOfBinaryTree(TreeNode* root) {
dfs(root->left, true, false);
dfs(root->right, false, true);
vector<int> res = {root->val};
for (auto left : lb) res.push_back(left);
for (auto child : children) res.push_back(child);
int m = rb.size();
for (int i = m - 1; i >= 0; --i) res.push_back(rb[i]);
return res;
}
};