当前位置:网站首页>【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边栏推荐
- Redis installation under windows and Linux systems
- How to initialize views when loading through storyboards- How is view initialized when loaded via a storyboard?
- 远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
- Shell script import and export data
- Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
- First knowledge of database
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
- Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
- 首发!!lancet饿了么官方文档
- Distributed task scheduling XXL job
猜你喜欢

【声明】关于检索SogK1997而找到诸多网页爬虫结果这件事

Principles of several common IO models

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

uploads-labs靶场(附源码分析)(更新中)

Microservices Seata distributed transactions

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

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)
![[redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)](/img/1f/3dd95522b8d5f03dd763a6779e3db5.jpg)
[redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)

Nifi from introduction to practice (nanny level tutorial) - flow

Microservice sentinel flow control degradation
随机推荐
[combinatorics] combinatorial identities (sum of variable terms 3 combinatorial identities | sum of variable terms 4 combinatorial identities | binomial theorem + derivation to prove combinatorial ide
Rk3399 platform development series explanation (WiFi) 5.54. What is WiFi wireless LAN
EditText request focus - EditText request focus
App mobile terminal test [4] APK operation
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)
How to thicken the brush in the graphical interface
pycharm错Error updating package list: connect timed out
Please be prepared to lose your job at any time within 3 years?
Colab works with Google cloud disk
Remote file contains actual operation
Microservice sentinel flow control degradation
Mb10m-asemi rectifier bridge mb10m
First knowledge of database
Expression of request header in different countries and languages
面试官:JVM如何分配和回收堆外内存
Shell script import and export data
分布式事务(Seata) 四大模式详解
Break through 1million, sword finger 2million!
无心剑中译泰戈尔《漂鸟集(1~10)》
探索Cassandra的去中心化分布式架构