当前位置:网站首页>Leetcode94-二叉树的中序遍历详解
Leetcode94-二叉树的中序遍历详解
2022-07-24 08:53:00 【白羊by】
往期博客:
目录
题目
给定一个二叉树的根节点
root,返回 它的 中序 遍历 。
题目分析
已知:二叉树的根节点
目的:中序遍历
示例
示例1

输入:root = [1,null,2,3] 输出:[1,3,2]
示例2
输入:root = [] 输出:[]
示例3
输入:root = [1] 输出:[1]
解析
递归法
中序遍历就是按左子树—>根节点—>右子树的顺序遍历二叉树,而对于左右子树同样按同样的方式进行遍历,直到遍历完整棵树。
对于如图所式的二叉树

根节点1有左子树和右子树,根节点2和3同样有左子树和右子树

所以遍历过程为:4—>2—>5—>1—>6—>3—>7
对于递归来说,就是先找整个二叉树最左边的子节点,这个最左边的子节点就是不再有左孩子的节点,对于下图就是节点4,绿色箭头表示递归,节点4没有左右孩子节点,此时就要递归到2节点的位置,遍历2节点的右孩子即5节点,5节点无左右孩子便递归到2节点,由于2节点左右孩子以全部遍历,所以再进行递归到1节点,此时整个二叉树的左子树全部遍历完毕。

同理,遍历二叉树的整个右子树

迭代法
迭代法是使用栈先进后出的思想遍历二叉树,首先遍历左子树的所有左孩子,并将左孩子加入栈中,直到最后一个左孩子不再有左右节点为止,此时再从栈中取出节点放入结果集。
具体如图所式,遍历左子树的左孩子,并加入栈中,到4节点时不再有左右节点,此时取出4和2加入结果集,到2节点时存在右孩子,将右孩子5节点加入栈中,5节点不再有左右孩子,将5从栈中取出加入结果集,最后将1取出加入结果集,此时,左子树以全部遍历完。

同理,遍历右子树

代码
递归法
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
result = []
self.helper(root, result)
return result
def helper(self, node, result):
if node is None:
return
self.helper(node.left, result)
result.append(node.val)
self.helper(node.right, result)迭代法
class Solution:
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
result = []
stack = []
cur = root
while cur or stack:
if cur:
stack.append(cur)
cur = cur.left
else:
cur = stack.pop()
result.append(cur.val)
cur = cur.right
return result边栏推荐
- WordPress free theme: document, making reading more convenient
- Is yuancosmos hype? Or the future
- 2、 Encapsulation and tool classes for adding, deleting, modifying and checking in midway
- Selenium webdriver page refresh
- Digital collection =nft? Have you entered the digital collection?
- 利用opencv 做一个简单的人脸识别
- Rank 3 and count from 0 to 9. How many times does each number appear in total, and how many times does each number appear in the tens of hundreds,
- JS problem summary
- First acquaintance with JVM
- 看了纪录片《埃达克岛的海盗宝藏》,想到我国历史上的遗失秘宝
猜你喜欢

Data collection solution for forestry survey and patrol inspection

Okaleido tiger NFT is about to log in to binance NFT platform, and the era of NFT rights and interests is about to start

超全总结:Go语言如何操作文件

脉脉网友出了道 Go 面试题,你能答对吗?

Use the bark app to realize the process of pushing messages to mobile phones

Houdini official HDA sidefx labs installation

Is yuancosmos hype? Or the future

安装软件时提示【An error occurred while trying to create a file in the destination directory: 拒绝访问】的解决方法

Paclitaxel loaded tpgs reduced albumin nanoparticles /ga-hsa gambogic acid human serum protein nanoparticles

How to use redis' publish subscribe (MQ) in.Netcore 3.1 project
随机推荐
3587. 连通图(吉林大学考研机试题)
Unity C#工具类 ArrayHelper
Four data interaction modes of go grpc
On express framework
Houdini 笔记
Porting boa server on imx6ull
Functions of tiktok enterprise number
Pulse netizens have a go interview question, can you answer it correctly?
Taking advantage of the momentum, oceanbase promotes the lean growth of digital payment
Typora prompt [this beta version of typora is expired, please download and install a new version]
[emotion] what is "excellent"
Rocky基础-Shell脚本基础知识
Treap
读写锁、共享锁、独占锁
林业调查巡检数据采集解决方案
Wargames NATAS (16-19) problem solving essays
Larave uses sanctum for API authentication
C语言练习题目+答案:
Basic use of Nacos (2) -- Nacos configuration center
脉脉网友出了道 Go 面试题,你能答对吗?