LeetCode:94. Binary Tree Inorder Traversal

【100%通过率】华为OD机试真题 Python 实现【水库蓄水问题】【2022.11 Q4 新题】 给定一个长度为 n 的整数数组height。数组的元素表示山的高度,选择两个元素作为水库的边界,求蓄水量的最大值并输出蓄水量最大时的边界下标(蓄水量相同时输出下标较近的)。 阅读详情

LeetCode:94. Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values.

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?

中序遍历一棵二叉树。

思路一:递归法

从根节点开始分别递归遍历某个结点左右子树,这个过程中如果这个个结点不为空,直接入队。最后这个队列里的元素顺序就是中序遍历的顺序。

Python 代码实现

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

class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        res = []
        self.helper(root,res)
        return res
    
    def helper(self, root, res):
        if (root != None):
            if (root.left != None):
                self.helper(root.left, res)
                
            res.append(root.val)
            
            if (root.right != None):
                self.helper(root.right, res)
思路二:迭代法

用一个辅助栈改写一下上面的递归方法。

Python 代码实现

class Solution:
    def inorderTraversal(self, root: TreeNode) -> List[int]:
        res = []
        stack = []
        cur = root
        while (cur != None or len(stack) > 0):
            while(cur != None):
                stack.append(cur)
                cur = cur.left
            cur = stack.pop()
            res.append(cur.val)
            cur = cur.right
        return res

THE END.

广东省道路工程检测练习试题(判断题)真题 按下式计算小于某粒径颗粒质量百分数:X=(A/B)x100( )49.、挖坑灌砂法适用于在现场测定基层(或底基层)、砂石路面及路基土的各种材料压实层的密度和压实度,但不适用于填石路堤等有大孔洞或大孔隙材料的压实度检测。95.挖坑灌砂法适用于在现场测定基层(或底基层)、砂石路面及路基土的各种材料压实层的密度和压实度,但不适用于填石路堤等有大孔洞或大孔隙材料的压实度检测( )107.以干密度 pd 为纵坐标,含水率w为横坐标,绘 pd-w 关系曲线,曲线上峰值点的纵、横坐标分别为最大干密度和最佳含水率( ) 阅读详情

相关推荐

Fortran环境配置:VS2022+Intel oneAPI安装全流程指南

Fortran环境配置:VS2022+Intel oneAPI安装全流程指南

shuyisuisuinian的博客 2万+

LeetCode94:Binary Tree Inorder Traversal

Given a binary tree, return theinordertraversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] Follow up:Recursive solution is trivial, could y...

卷卷萌的博客 481

麒麟V10系统下CoreDump文件自动归档与快速定位指南(附coredumpctl实战)

本文详细介绍了在麒麟V10操作系统下,如何利用systemd-coredump机制和coredumpctl工具实现CoreDump文件的自动化管理与高效调试。内容涵盖从系统架构解析、自动化配置策略,到使用coredumpctl进行快速查询、过滤与深度分析的实战指南,旨在帮助运维人员和开发者快速定位系统崩溃的根本原因,提升问题排查效率。

gg901的博客 234

LeetCode 94:Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solutio

henryzhihua 2506

华为无线AP+AC(ensp)

VLAN 100--192.168.100.0/24为AP的管理IP地址VLAN10--192.168.10.0/24为无线用户获取的IP地址在DHCP Server(LSW3)上查看地址分配情况。

abcdlalal的博客 1万+

新手必看:手把手教你如何通过Github提交CVE漏洞(附完整截图流程)

本文为安全研究新手提供了一份详尽的CVE漏洞提交实战指南。文章重点介绍了如何通过Github平台,从漏洞发现、验证、撰写专业报告到最终提交CVE申请的全流程,并附有完整的截图和沟通模板,帮助初学者迈出负责任漏洞披露的第一步。

ttt77的博客 1267

LeetCode 94: Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes’ values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] Iteration class Solution: def inorderTraversal...

forever_zzx的专栏 188

不写代码,Cursor 10分钟开发1个APP!保姆级教程喂饭来了

做出第一个能运行的app,那种成就感真的很棒。虽然过程可能有点折腾,但只要耐心一点,问题都能解决。记住,遇到问题不要慌,现在有AI帮忙,大部分问题都能快速解决。重要的是迈出第一步,开始动手实践。你的第一个app可能很简单,但这是一个很好的开始。随着经验积累,你能做出的东西会越来越酷。现在就开始行动吧,说不定下个月你就能向朋友炫耀你自己做的app了!

chenhong007的博客 1570

leetcode-94:Binary Tree Inorder Traversal (Java)

Binary Tree Inorder Traversal  即二叉树的中序遍历。 常见的有两种方法:递归和循环,其中递归调用的栈空间为树的高度,一般为o(logn),循环方式需要开辟一个栈来保存元素,空间复杂度也是o(logn) tips: 递归比循环耗时,递归:400ms,循环:220ms 另外还以一种方式遍历二叉树: Morr

懒人部落 1339

【Linux】实际项目应该如何完成?--安防监控系统项目的总结

到目前为止我也做个5、6个嵌入式项目,但以往都是从零开始自己做,也就是毫无计划,想到那里做那里,有想法就会去实现,就这样导致我碰到了很多坑,也消耗了我许多时间,当然,不是说这样不好,因为在其中我也得到了一个很好的锻炼与练习,但是这不适用于在工作中去这样做。...

修成真的博客 6749

AI Agent Harness与SCADA系统集成

简单来说,AI Agent Harness是Industrial AI Agent的“中央调度室+装备库+知识库+安全网”把不同技术栈、不同部署环境(云/边/端)、不同功能(感知、推理、决策、执行、验证)的“零散Agent”组装成具备特定工业场景能力的Agent Team(如聚合反应根因诊断Agent Team);管理Agent Team的生命周期(创建、初始化、调度、监控、销毁);提供统一的工业多工具调用接口(对接SCADA、MES、PLC、DCS、巡检机器人、数字孪生平台等);内置。

欢迎来到我的CSDN空间!这里聚焦AI大模型应用实战,分享前沿技术、实战案例与开发经验。 363

Leetcode: 94. Binary Tree Inorder Traversal

Description Given a binary tree, return the inorder traversal of its nodes' values. Example Given binary tree [1,null,2,3], 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, c...

anrui0096的博客 243

Gromacs 模拟昆虫 P450 解毒酶与农药结合完整指南,仅供参考!

🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。

**My Coding Family** 1271

Leetcode 94:Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes’ values. Example: Input: [1,null,2,3] 1 2 / 3 Output: [1,3,2] C++ vector<int> inorderTraversal(TreeNode* root) { vector&lt...

qq_34314736的博客 139

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

LeetCode: 94. Binary Tree Inorder Traversal

LeetCode: 94. Binary Tree Inorder Traversal 题目描述 Given a binary tree, return the inorder traversal of its nodes’ values. For example: Given binary tree [1,null,2,3], 1 \ 2 / 3 ...

杨领well的专栏 1255

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

LeetCode笔记:94. Binary Tree Inorder Traversal

左中右遍历二叉树

Minecraft 894

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

leetcode[94]:Binary Tree Inorder Traversal

Binary Tree Inorder TraversalGiven a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2].Note: Recursive

hahubaba的博客 253

Leetcode 94: Binary Tree Inorder Traversal

问题描述: Given the root of a binary tree, return the inorder traversal of its nodes’ values. 中序遍历 最终结果 = 中序遍历左子树 + 根 + 中序遍历右子树 (如果根存在) 技巧: 这里把list作为参数在递归函数之间传递,也就是说,整个程序来回来去都是写在这一个list中 代码如下: class Solution { public List<Integer> inorderTraversal(Tr

weixin_51401145的博客 193

leetcode: 树 94. Binary Tree Inorder Traversal

题目 Given a binary tree, return theinordertraversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output: [1,3,2] Follow up:Recursive solution is trivial, cou...

qq_22498427的博客 113

LeetCode 94:二叉树的中序遍历 Binary Tree Inorder Traversal

题目: 给定一个二叉树,返回它的中序 遍历。 Given a binary tree, return the inorder traversal of its nodes’ values. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? Follow up: Recu...

爱写Bug 307

LeetCode94:Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, return [1,3,2].Note: Recursive solution is trivial, could you do it iteratively?二叉树的

737

leetcode(94):Binary Tree Inorder Traversal

题目Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2].思路二叉

Stay hungry,Stay foolish 493
上一篇: LeetCode:647. Palindromic Substrings
下一篇: LeetCode:238. Product of Array Except Self
XP-Code
博客等级 码龄12年 113粉丝 194原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值