[leetcode] 366. Find Leaves of Binary Tree @ python

FPGA进位链(Carry Chain)实战:从Xilinx CARRY8原语到性能优化全解析 本文深入解析了FPGA进位链(Carry Chain)的原理与应用,重点剖析了Xilinx CARRY8原语的端口功能与内部机制。通过实测数据揭示了进位链的真实延迟特性与温度影响,并详细阐述了如何利用DI和S信号进行动态路径优化、实现减法运算与条件进位选择,从而有效提升设计性能。最后,文章探讨了系统级的流水线拆分、布局约束等实战优化策略与常见陷阱。 阅读详情

原题

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:

Input: [1,2,3,4,5]

      1
     / \
    2   3
   / \     
  4   5    

Output: [[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:

       []        
    

解法1

递归+分治法. 构建d存储每层的节点值, 当节点为空时, 为第0层, 当根节点时, 为第1层, 以此类推. 构造getLevel函数, 分别求得左右子节点的层数, 当前节点的层数为1 + max(left, right), 然后更新字典. 最后遍历字典的键, 将值加入res.

Time: O(n)
Space: O(n)

代码

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def findLeaves(self, root: 'TreeNode') -> 'List[List[int]]':
        def getLevel(root, d):
            if not root:
                return 0
            left = getLevel(root.left, d)
            right = getLevel(root.right, d)
            level = 1 + max(left, right)
            d[level].append(root.val)
            return level
        
        d = collections.defaultdict(list)
        getLevel(root, d)
        res = []
        for k in sorted(d.keys()):
            res.append(d[k])
        return res

解法2

同解法1

代码

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def findLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        d = collections.defaultdict(list)
        ans = []
        
        def traverse(root):
            # return the level of the nodes
            if not root:
                return 0
            left = traverse(root.left)
            right = traverse(root.right)
            level = max(left, right) + 1
            d[level].append(root.val)
            return level
        
        traverse(root)
        for i in range(1, len(d)+1):
            ans.append(d[i])
        return ans
GD32F470移植LWIP协议,PHY芯片DP83848I 也就是说前24位是有规定的,其中48位MAC中的前几位也是有着对应的含义的。2、旧版LWIP(如1.4.1)存在DHCP状态机卡死问题,建议升级至2.1.3+。 阅读详情

相关推荐

保姆级教程:用Multisim仿真快速上手二极管、MOSFET和IGBT的开关特性

本文提供了一份详细的Multisim仿真教程,帮助读者快速掌握二极管、MOSFET和IGBT的开关特性。通过搭建实验电路、观察波形特征和参数影响分析,深入解析了反向恢复过程、米勒平台现象和拖尾电流效应。适合电力电子初学者和工程师通过仿真建立直观理解,提升电路设计能力。

weixin_29290863的博客 498

LeetCode-Python- 366. 寻找完全二叉树的叶子节点

给你一棵完全二叉树,请按以下要求的顺序收集它的全部节点: 依次从左到右,每次收集并删除所有的叶子节点 重复如上过程直到整棵树为空 示例: 输入: [1,2,3,4,5] 1 / \ 2 3 / \ 4 5 输出: [[4,5,3],[2],[1]] 解释: 1. 删除叶子节...

Keep Coding 1207

Cursor 使用教程(2025年更新版)

官方Cursor详细使用教程在“扩展学习”

我是赛博AI Lewis 7082

LeetCode 366. Find Leaves of Binary Tree - 二叉树(Binary Tree)系列题9

一个节点在第几层被剥离决定了它在结果中的位置,而一个节点是在第几层被剥离又取决于它的左右子树的最大高度

hgq522的博客 257

【vLLM-性能洞察】基于Ascend Profiler的端到端推理瓶颈定位与优化实践

本文详细介绍了如何利用Ascend Profiler对vLLM-Ascend大模型推理服务进行端到端的性能瓶颈定位与优化。通过实战案例,阐述了从数据采集、可视化分析到诊断低效算子、内存管理、计算通信重叠及框架调度等常见瓶颈的全过程,为基于昇腾NPU的AI推理性能调优提供了系统性的方法论和实用工具指南。

cc789的博客 521

初探802.11协议(6)——Wi-Fi 6新特性简介

Wi-Fi 6新机制(特性)介绍

FanFF的博客 1522

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 / \ ...

leetcode 解题思路 357

Go语言全面解析:从入门到精通

Go语言是由Google开发的一种静态类型、编译型编程语言,具有简洁、高效、安全的特点。它特别适合高并发服务、云原生应用和网络编程等场景。Go采用Goroutine和Channel实现轻量级并发,性能接近C/C++。与其他语言相比,Go语法更简洁但类型更严格,编译速度快且跨平台支持好。虽然Go支持多种设计模式和编程范式,但其面向对象实现方式与传统语言不同。标准库丰富且拥有Gin、GORM等企业级框架。核心语法包括包管理、变量声明、函数定义、流程控制等,适合构建高性能后端服务和系统工具。

博哥哥的博客 4925

android编译系统 - findleaves.sh

<br />#!/bin/bash<br /> <br />#  build/tools/findleaves.sh<br /><br /><br />set -o nounset  # fail when dereferencing unset variables<br /># 扩展一个未设置的变量时显示一个错误信息<br /><br />set -o errexit   # fail if any subcommand fails<br /># 当命令返回一个非零退出状态(失败)时退出<br /><br

zgolee的专栏 1531

Matlab波动光光学系统

波动光学是一种研究光波传播过程的理论,按照其处理的信息类型不同,可以分为相干和非相干波动光学,相干波动光学可以用作特定光学系统的分析和设计,其主要依赖物理光学和电磁波的基本原理,以及光学干涉和衍射的经验方程式,其中光波传播过程是以复数形式表示的,包括振幅和相位两个方面。近年来光学技术得到了广泛的应用和发展,波动光光学系统是其中一种重要的光学系统,它主要是利用波动光学原理,通过计算机模拟实现各种光学系统的设计、分析和优化,可应用于大气光传输、光束整形、分析系统的衍射和干涉特性,包括利用。

weixin_50547796的博客 260

数学史学习资料:史前数学

著名物理学家伽莫夫(宇宙大爆炸理论以及核聚变理论的提出者)在他的科普书《从1到无穷大》中举了这样一个例子。两个原始部落的酋长比赛,看谁说的数字大。第一个酋长说了三,第二个酋长想了半天,然后说你赢了。你今天听起来觉得这两个人真可笑,至少说出四,岂不就赢了?但是对这两位酋长来讲,所拥有的东西就很少超过三个,比三个多的东西他们就觉得没有必要数清楚了,就用许多来形容了。也就是说,他们生活的环境限制了他们的认知。

howard2005的专栏 902

Android 编译脚本解析 findleaves.py 解读

最近在分析Android编译,加上之前确实写过关于findleaves.py的应用,今天找时间分析一下原理,也算是学习  #!/usr/bin/env python # # Copyright (C) 2009 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "Lic...

u011494885的博客 690

STM32G0/G4电机控制中三角函数与浮点运算优化实战

三角函数(sin/cos)和浮点运算是电机矢量控制(FOC)的核心基础,直接影响Clark/Park变换、SVPWM及观测器的实时性与精度。在资源受限的MCU上,标准math.h库因无硬件FPU支持而引发严重时序抖动,导致电流环延迟、转矩脉动增大甚至系统复位。通过硬件加速(如STM32G4的CORDIC协处理器)或定制化定点算法(如STM32G0的查表+线性插值),可将sin/cos计算从数十微秒压缩至1–3 μs量级,提升30倍以上效率。该方案广泛适用于BLDC/PMSM驱动、伺服控制及高动态响应嵌入式运

weixin_35751412的博客 109

LeetCode in Python 366. Find Leaves of Binary Tree

# 366. Find Leaves of Binary Tree # Example: # Given binary tree # 1 # / \ # 2 3 # / \ # 4 5 # Returns [4, 5, 3], [2], [1]. class Solution(o...

weixin_30597269的博客 160

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Leetcode 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 / \ ...

weixin_30487201的博客 89

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

[LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点

Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Example:Given binary tree 1 / \ 2 3 ...

weixin_30715523的博客 168

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Android的build文件findleaves.py分析

subdir_makefiles := \         $(shell build/tools/findleaves.py --prune=$(OUT_DIR) --prune=.repo --prune=.git $(subdirs) Android.mk) 可以看出传输的参数是--prune=$(OUT_DIR)    --prune=.repo --prune=.git $(s

9527的专栏 1258

366. Find Leaves of Binary Tree

366. Find Leaves of Binary Tree方法1: dfs方法2: peel the onion易错点Complexity 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 e...

wilzxu的博客 270

Find Leaves of Binary Tree - LintCode

描述 给定一个二叉树,像这样收集树节点:收集并移除所有叶子,重复,直到树为空。 样例 Given binary tree: 1 / \ 2 3 / \ 4 5 Returns [[4, 5, 3], [2], [1]]. 思路 #ifndef C650_H #define C650_H #include&lt;iostream&g...

kane的博客 327

LeetCode 366. Find Leaves of Binary Tree

原题网址:https://leetcode.com/problems/find-leaves-of-binary-tree/ Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Example:

James Pan 2867

[leetcode] 366. Find Leaves of Binary Tree 解题报告

题目链接: https://leetcode.com/problems/find-leaves-of-binary-tree/ Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps until the tree is empty. Examp

小榕流光的专栏 4981

【LeetCode - 366】寻找二叉树的叶子节点

【中等】树、DFS

学哥斌的博客 3766

Compute the height of a binary tree

#include #include #include #include using namespace std; typedef int T; struct BinaryNode{ T data; BinaryNode *left, *right; BinaryNode(T d, BinaryNode *l=NULL, BinaryNode* r=NULL):data(

nemo的博客 456

Compute the height of a binary search tree

Given a list of integers, we can build a binary search tree by inserting them one by one, then compute the height. The input has two lines: the first line is an integer n, the second line contains n i

nemo的博客 593

Python学习(Leetcode 算法题解【21~40】)

343. Integer Break题目: Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.For example

Hans__Yang的博客 1738
上一篇: [leetcode] 198. House Robber @ python
下一篇: [leetcode] 763. Partition Labels @ python
闲庭信步的空间
博客等级 码龄9年 86粉丝 416原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值