当前位置:网站首页>【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边栏推荐
- "Everyday Mathematics" serial 56: February 25
- pycharm错Error updating package list: connect timed out
- 深入理解 SQL 中的 Grouping Sets 语句
- 切入点表达式
- Expression of request header in different countries and languages
- 分布式事务(Seata) 四大模式详解
- Record a jar package conflict resolution process
- The difference between calling by value and simulating calling by reference
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
- [combinatorics] combinatorial identity (sum of variable upper terms 1 combinatorial identity | summary of three combinatorial identity proof methods | proof of sum of variable upper terms 1 combinator
猜你喜欢

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)

TCP拥塞控制详解 | 3. 设计空间

Record a jar package conflict resolution process

Microservice sentinel flow control degradation

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)

Explore Cassandra's decentralized distributed architecture

Shell script import and export data

From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing

关于网页中的文本选择以及统计选中文本长度

Initial test of scikit learn Library
随机推荐
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
[statement] about searching sogk1997 and finding many web crawler results
SVN使用规范
关于网页中的文本选择以及统计选中文本长度
Custom plug-in construction and use of QT plug-in
Microservice sentinel flow control degradation
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
潘多拉 IOT 开发板学习(HAL 库)—— 实验5 外部中断实验(学习笔记)
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
Batch files: list all files in a directory with relative paths - batch files: list all files in a directory with relative paths
How to thicken the brush in the graphical interface
First!! Is lancet hungry? Official documents
How to initialize views when loading through storyboards- How is view initialized when loaded via a storyboard?
[combinatorics] combinatorial identities (sum of variable terms 3 combinatorial identities | sum of variable terms 4 combinatorial identities | binomial theorem + derivation to prove combinatorial ide
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
"Everyday Mathematics" serial 56: February 25
Microservice - Nacos registration center and configuration center
Client does not support authentication protocol requested by server; consider upgrading MySQL client
用同花顺炒股开户安全吗?
高等数学(第七版)同济大学 习题2-1 个人解答