当前位置:网站首页>【LeetCode】94. Middle order traversal of binary tree
【LeetCode】94. Middle order traversal of binary tree
2022-07-03 16:17:00 【onlywishes】
Their thinking :
( One )
The middle order of the binary tree is Left root right , First traverse the left half , Then the root node , And the right half .
The left half can be divided according to the initial state , The same is true for the right half , You can use recursion
Take the left node of the root node first and recurse , If the left node is empty, it means to the far left , At this time, this node is the root node of this layer , From left root to right , Add the root node without recursion to the stack , After recursion, the right child node of the root node , If not, return to
When implementing recursively , It's the function that calls itself , Nested layer by layer , operating system / Virtual machine automatically helps us use Stack To save each called function
Recursion is a tree structure , Literally, it can be understood as repetition “ Recurrence ” and “ Return to ” The process of , When “ Recurrence ” When you reach the bottom, you start “ Return to ”, The process is equivalent to the depth first traversal of the tree .
Call the process :
dfs(root.left)
dfs(root.left)
dfs(root.left)
by null return
Print nodes
dfs(root.right)
dfs(root.left)
dfs(root.left)
........
Recursive implementation
res = [] # Store node values
def dg(root):
if not root: # If the current node is empty , return , Add node value
return
dg(root.left) # Follow the left , Join root , Right way traversal
res.append(root.val)
dg(root.right)
dg(root) # Call yourself
return res
( Two )
Use iteration : Activities that repeat the feedback process , The result of each iteration will be taken as the initial value of the next iteration .(A Repeated calls to B)
Iteration is a Ring structure , From the beginning , Every iteration goes through the loop , And update the status , Iterations until the end state is reached .
Explain : Start from the root node to the left node , Every node is stored in the stack , The last entry node is the leftmost node , And then out of the stack , Save its element value , View its right node , Some words , Push , Start from this node to the left node , If there is no right node , Keep going out of the stack , loop , Until all nodes are put into and out of the stack , end
if not root:
return []
res = [] # Store node values
stack = [] # Save the node temporarily , It's like a stack
cur = root # Set the pointer
while cur or stack: # When cur When the pointing value and stack are empty, there are no elements
while cur : # Add the path from root to left to the stack in turn
stack.append(cur)
cur = cur.left
node = stack.pop() # Reach the end , Just take out the value of the last node in the stack and add it to the result res in
res.append(node.val)
cur = node.right # Looking at the right child node of this node , loop
return res # When all nodes are added to the stack , After the last stack ,res
边栏推荐
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
- Please be prepared to lose your job at any time within 3 years?
- EditText request focus - EditText request focus
- Q2 encryption market investment and financing report in 2022: gamefi becomes an investment keyword
- App mobile terminal test [4] APK operation
- Qt插件之自定义插件构建和使用
- MongoDB 的安装和基本操作
- How idea starts run dashboard
- 分布式事务(Seata) 四大模式详解
- 记一次jar包冲突解决过程
猜你喜欢
The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)
Microservice API gateway
探索Cassandra的去中心化分布式架构
Low level version of drawing interface (explain each step in detail)
2022年Q2加密市场投融资报告:GameFi成为投资关键词
Deep understanding of grouping sets statements in SQL
Colab works with Google cloud disk
MB10M-ASEMI整流桥MB10M
Three dimensional reconstruction of deep learning
Salary 3000, monthly income 40000 by "video editing": people who can make money never rely on hard work!
随机推荐
Stm32f103c8t6 firmware library lighting
Shell script import and export data
PHP二级域名session共享方案
Caching mechanism of Hibernate / session level caching mechanism
几种常见IO模型的原理
Nine ways to define methods in scala- Nine ways to define a method in Scala?
用通达信炒股开户安全吗?
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
NSQ源码安装运行过程
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
MongoDB 的安装和基本操作
How to thicken the brush in the graphical interface
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
Semi supervised learning
深入理解 SQL 中的 Grouping Sets 语句
"Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
[combinatorics] non descending path problem (outline of non descending path problem | basic model of non descending path problem | non descending path problem expansion model 1 non origin starting poi
Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
Microservice - Nacos registration center and configuration center
探索Cassandra的去中心化分布式架构