-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
366.cpp
54 lines (54 loc) · 1.88 KB
/
366.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
__________________________________________________________________________________________________
class Solution {
public:
vector<vector<int>> findLeaves(TreeNode* root) {
vector<vector<int>> res;
helper(root, res);
return res;
}
int helper(TreeNode* root, vector<vector<int>>& res) {
if (!root) return -1;
int depth = 1 + max(helper(root->left, res), helper(root->right, res));
if (depth >= res.size()) res.resize(depth + 1);
res[depth].push_back(root->val);
return depth;
}
};
__________________________________________________________________________________________________
class Solution {
public:
vector<vector<int>> findLeaves(TreeNode* root) {
vector<vector<int>> res;
while (root) {
vector<int> leaves;
root = remove(root, leaves);
res.push_back(leaves);
}
return res;
}
TreeNode* remove(TreeNode* node, vector<int>& leaves) {
if (!node) return NULL;
if (!node->left && !node->right) {
leaves.push_back(node->val);
return NULL;
}
node->left = remove(node->left, leaves);
node->right = remove(node->right, leaves);
return node;
}
};
__________________________________________________________________________________________________
class Solution {
public:
vector<vector<int>> findLeaves(TreeNode* root) {
if (!root) return {};
vector<vector<int>> left = findLeaves(root->left), right = findLeaves(root->right);
vector<vector<int>> res = (left.size() >= right.size()) ? left : right;
vector<vector<int>> t = (left.size() >= right.size()) ? right : left;
for (int i = 0; i < t.size(); ++i) {
res[i].insert(res[i].begin(), t[i].begin(), t[i].end());
}
res.push_back({root->val});
return res;
}
};