366. Find Leaves of Binary Tree

366. Find Leaves of Binary Tree

Description

Given the root of a binary tree, collect a tree’s nodes as if you were doing this:

  • Collect all the leaf nodes.
  • Remove all the leaf nodes.
  • Repeat until the tree is empty.

Example 1:

1
2
3
4
Input: root = [1,2,3,4,5]
Output: [[4,5,3],[2],[1]]
Explanation:
[[3,5,4],[2],[1]] and [[3,4,5],[2],[1]] are also considered correct answers since per each level it does not matter the order on which elements are returned.

Example 2:

1
2
Input: root = [1]
Output: [[1]]

Constraints:

  • The number of nodes in the tree is in the range [1, 100].
  • -100 <= Node.val <= 100

Hints/Notes

  • N/A

Solution

Language: C++

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
/**
* 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<vector<int>> res;

vector<vector<int>> findLeaves(TreeNode* root) {
traverse(root);
return res;
}

int traverse(TreeNode* root) {
if (!root) {
return 0;
}

int leftDepth = traverse(root->left);
int rightDepth = traverse(root->right);

int depth = max(leftDepth, rightDepth) + 1;

if (depth > res.size()) {
res.push_back({root->val});
} else {
res[depth - 1].push_back(root->val);
}

return depth;
}
};