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 
          1
         /         2   3
       / \     
      4   5    
 
Returns [4, 5, 3], [2], [1].
Explanation:
1. Removing the leaves [4, 5, 3] would result in this tree:
          1
         / 
        2          
 
2. Now removing the leaf [2] would result in this tree:
          1          
 
3. 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

366. Find Leaves of Binary Tree

 

 这题还比较新(对我),解决办法是计算每个node的height,子节点是0,网上走,root = Math。max(left,right)+ 1。root==null就返回-1.

然后看是否需要加入新的ArrayList,然后把相同height的node加到对应的list中

366. Find Leaves of Binary Tree

 

 

366. Find Leaves of Binary Tree

上一篇:记一次BUG修复


下一篇:关于ECharts的使用总结