首页 > 其他 > 详细

366. Find Leaves of Binary Tree

时间:2021-07-03 10:52:16      阅读:18      评论:0      收藏:0      [点我收藏+]
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

技术分享图片

 

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

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

技术分享图片

 

 

366. Find Leaves of Binary Tree

原文:https://www.cnblogs.com/wentiliangkaihua/p/14965487.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!