zoukankan      html  css  js  c++  java
  • 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

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

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

  • 相关阅读:
    centos7 主从dns配置 bind服务
    Linux的启动流程
    shell脚本编程数组
    shell脚本编程进阶
    计划任务
    进程管理
    Linux网络配置
    计算机网络基础
    磁盘存储和文件系统
    软件包管理
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/14965487.html
Copyright © 2011-2022 走看看