366. Find Leaves of Binary Tree
作者:互联网
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
Given binary tree
1 / \ 2 3 / \ 4 5Returns
[4, 5, 3], [2], [1]
.
Explanation:
1. Removing the leaves [4, 5, 3]
would result in this tree:
1 / 22. Now removing the leaf
[2]
would result in this tree:
13. Now removing the leaf
[1]
would result in the empty tree:
[]Returns
[4, 5, 3], [2], [1]
.
https://zhuhan0.blogspot.com/2017/05/leetcode-366-find-leaves-of-binary-tree.html
https://www.youtube.com/watch?v=2vwTmHTL1Mk&ab_channel=XavierElon
这题还比较新(对我),解决办法是计算每个node的height,子节点是0,网上走,root = Math。max(left,right)+ 1。root==null就返回-1.
然后看是否需要加入新的ArrayList,然后把相同height的node加到对应的list中
标签:Binary,Given,would,binary,Leaves,tree,result,366,leaves 来源: https://www.cnblogs.com/wentiliangkaihua/p/14965487.html