当前位置:网站首页>【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边栏推荐
- Unity项目优化案例一
- A Fei's expectation
- Extraction of the same pointcut
- Shell script import and export data
- 0214-27100 a day with little fluctuation
- 一些事情的反思
- Détails du contrôle de la congestion TCP | 3. Espace de conception
- Nine ways to define methods in scala- Nine ways to define a method in Scala?
- 突破100万,剑指200万!
- Distributed task scheduling XXL job
猜你喜欢

TCP擁塞控制詳解 | 3. 設計空間

Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
![[web security] - [SQL injection] - error detection injection](/img/18/5c511871dab0e5c684b6b4c081c061.jpg)
[web security] - [SQL injection] - error detection injection

First knowledge of database

Project -- high concurrency memory pool

The accept attribute of the El upload upload component restricts the file type (detailed explanation of the case)

Microservices Seata distributed transactions

About text selection in web pages and counting the length of selected text

Microservice sentinel flow control degradation
![SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]](/img/3b/7523eca5bbcdbba29d9b7f6e4791a5.jpg)
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
随机推荐
Stm32f103c8t6 firmware library lighting
Caching mechanism of Hibernate / session level caching mechanism
[statement] about searching sogk1997 and finding many web crawler results
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
阿飞的期望
Microservice API gateway
用同花顺炒股开户安全吗?
特征多项式与常系数齐次线性递推
[redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
Unity project optimization case 1
2022年Q2加密市场投融资报告:GameFi成为投资关键词
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (III)
嵌入式开发:避免开源软件的7个理由
Explore Cassandra's decentralized distributed architecture
From the 18th line to the first line, the new story of the network security industry
PHP CI(CodeIgniter)log级别设置
Qt插件之自定义插件构建和使用
About text selection in web pages and counting the length of selected text
TCP拥塞控制详解 | 3. 设计空间
ThreeJS 第二篇:顶点概念、几何体结构