366. Find Leaves of Binary Tree (Medium)
Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.
Example: Given binary tree
1
/ \
2 3
/ \
4 5
Returns [4, 5, 3], [2], [1]
.
Explanation:
Removing the leaves
[4, 5, 3]
would result in this tree:1 / 2
Now removing the leaf
[2]
would result in this tree:1
Now removing the leaf
[1]
would result in the empty tree:[]
Returns
[4, 5, 3], [2], [1]
.
Solution 1: DFS on depth 3ms
这道题给了我们一个二叉树,让我们返回其每层的叶节点,就像剥洋葱一样,将这个二叉树一层一层剥掉,最后一个剥掉根节点。那么题目中提示说要用DFS来做,思路是这样的,每一个节点从左子节点和右子节点分开走可以得到两个深度,由于成为叶节点的条件是左右子节点都为空,所以我们取左右子节点中较大值加1为当前节点的深度值,知道了深度值就可以将节点值加入到结果res中的正确位置了,求深度的方法我们可以参见Maximum Depth of Binary Tree中求最大深度的方法,参见代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
int dfs(TreeNode* root, vector<vector<int>>& res) {
if (!root) return -1;
int depth = 1+max(dfs(root->left, res), dfs(root->right, res));
if (depth >= res.size()) res.push_back(vector<int>());
res[depth].push_back(root->val);
return depth;
}
public:
vector<vector<int>> findLeaves(TreeNode* root) {
vector<vector<int>> res;
dfs(root, res);
return res;
}
};
Solution 2: DFS w/o depth 3ms
下面这种DFS方法没有用计算深度的方法,而是使用了一层层剥离的方法,思路是遍历二叉树,找到叶节点,将其赋值为NULL,然后加入leaves数组中,这样一层层剥洋葱般的就可以得到最终结果了:
class Solution {
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;
}
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;
}
};