-
Notifications
You must be signed in to change notification settings - Fork 4
/
366-find-leaves-of-binary-tree.cpp
141 lines (110 loc) · 3.99 KB
/
366-find-leaves-of-binary-tree.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* 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) {}
* };
*/
// height calculate, sort, insert
class Solution {
public:
vector<pair<int, int>> pairs;
int getHeight(TreeNode *root) {
// return -1 for null nodes
if (!root) return -1;
// first calculate the height of the left and right children
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
// based on the height of the left and right children, obtain the height of the current (parent) node
int currHeight = max(leftHeight, rightHeight) + 1;
// collect the pair -> (height, val)
this->pairs.push_back({currHeight, root->val});
// return the height of the current node
return currHeight;
}
vector<vector<int>> findLeaves(TreeNode* root) {
this->pairs.clear();
getHeight(root);
// sort all the (height, val) pairs
sort(this->pairs.begin(), this->pairs.end());
int n = this->pairs.size(), height = 0, i = 0;
vector<vector<int>> solution;
while (i < n) {
vector<int> nums;
while (i < n && this->pairs[i].first == height) {
nums.push_back(this->pairs[i].second);
i++;
}
solution.push_back(nums);
height++;
}
return solution;
}
};
// keeping only vector, without sort
class Solution {
private:
vector<vector<int>> solution;
public:
int getHeight(TreeNode *root) {
// return -1 for null nodes
if (!root) return -1;
// first calculate the height of the left and right children
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
// based on the height of the left and right children,
// obtain the height of the current (parent) node
int currHeight = max(leftHeight, rightHeight) + 1;
// create space for node located at `currHeight` if not already exists
if (this->solution.size() == currHeight) this->solution.push_back({});
// insert the value at the correct position in the solution array
this->solution[currHeight].push_back(root->val);
// return the height of the current node
return currHeight;
}
vector<vector<int>> findLeaves(TreeNode* root) {
this->solution.clear();
getHeight(root);
return this->solution;
}
};
// traverse, mark
class Solution {
bool isLeaf(TreeNode* root) {
if (!root->left && !root->right) return true;
return false;
}
vector<vector<int>> extractedLeaves(TreeNode* root) {
vector<vector<int>> result;
while(root && !isLeaf(root)) {
auto leaves = getLeaves(root);
result.push_back(leaves);
}
if (root) result.push_back({root->val});
return result;
}
vector<int> getLeaves(TreeNode* root) {
vector<int> leaves;
if (root) preorder(root, leaves);
return leaves;
}
bool preorder(TreeNode* root, vector<int>& leaves) {
if (!root->left && !root->right) {
leaves.push_back(root->val);
return true;
}
auto isLeftLeaf = root->left ? preorder(root->left, leaves) : true;
auto isRightLeaf = root->right ? preorder(root->right, leaves) : true;
if (isLeftLeaf) root->left = NULL;
if (isRightLeaf) root->right = NULL;
return false;
}
public:
vector<vector<int>> findLeaves(TreeNode* root) {
return extractedLeaves(root);
}
};