当前位置:网站首页>Leetcode94 detailed explanation of middle order traversal of binary tree
Leetcode94 detailed explanation of middle order traversal of binary tree
2022-07-24 09:00:00 【Aries by】
Previous blogs :
Leetcode1- Detailed explanation of the sum of two numbers
Leetcode2- Detailed explanation of the code for adding two numbers
Leetcode20- Valid parentheses explain
Leetcode21- Merge two ordered linked lists
Leetcode22- Generation of valid parentheses
Leetcode24- Exchange the nodes in the linked list in pairs
Leetcode27- Remove element details
Leetcode46- Full arrangement and detailed explanation
Leetcode49- Detailed explanation of heterotopic grouping of letters
Leetcode53- Maximum subarray and explanation
Leetcode56- Detailed explanation of merging interval
LeetCode57- Insert interval explanation
Leetcode77- Detailed explanation of combination
Leetcode78- Subset explanation
Leetcode90- A subset of II Detailed explanation
Catalog
subject
Given the root node of a binary tree
root, return its Middle preface Traverse .
Topic analysis
It is known that : The root node of a binary tree
Purpose : In the sequence traversal
Example
Example 1

Input :root = [1,null,2,3] Output :[1,3,2]
Example 2
Input :root = [] Output :[]
Example 3
Input :root = [1] Output :[1]
analysis
Recursive method
Traversal in middle order is to press the left subtree —> The root node —> The order of the right subtree traverses the binary tree , The left and right subtrees are traversed in the same way , Until you walk through the whole tree .
For the binary tree as shown in the figure

The root node 1 There are left and right subtrees , The root node 2 and 3 There are also left subtrees and right subtrees

So the traversal process is :4—>2—>5—>1—>6—>3—>7
For recursion , That is to find the leftmost child node of the whole binary tree first , This leftmost child node is the node that no longer has left children , For the following figure, there are nodes 4, The green arrow indicates recursion , node 4 There are no left and right child nodes , At this point, we need to recurse to 2 Location of nodes , Traverse 2 The right child of the node is 5 node ,5 The node recurses to 2 node , because 2 Node left and right children to traverse all , So recurse to 1 node , At this time, the left subtree of the entire binary tree is traversed .

Empathy , Traverse the entire right subtree of a binary tree

Iterative method
Iterative method is to use the idea of stack first out to traverse binary tree , First, traverse all left children of the left subtree , And add the left child to the stack , Until the last left child no longer has left and right nodes , At this point, take the node from the stack and put it into the result set .
The details are shown in the figure , The left child traversing the left subtree , And add it to the stack , To 4 There are no more left and right nodes , At this point 4 and 2 Add result set , To 2 There are right children when nodes , Put the right child 5 Nodes are added to the stack ,5 Nodes no longer have left and right children , take 5 Take it out of the stack and add it to the result set , The final will be 1 Take out and add to the result set , here , The left subtree is traversed completely .

Empathy , Traversal right subtree

Code
Recursive method
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)Iterative method
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边栏推荐
- After two days of tossing and turning, I finally wrote my first fluent app, which almost tortured me into a nervous breakdown
- How to use redis' publish subscribe (MQ) in.Netcore 3.1 project
- 【汇编语言实战】(二)、编写一程序计算表达式w=v-(x+y+z-51)的值(含代码、过程截图)
- mysql URL
- Tiktok shop platform will take disciplinary measures against sellers who violate rules and policies
- Tiktok shop will add a new site, and the Singapore site will be launched on June 9
- Using OpenCV to do a simple face recognition
- Rocky basics shell script Basics
- TT ecosystem - cross border in-depth selection
- Replace the function of pow with two-dimensional array (solve the time overrun caused by POW)
猜你喜欢

【FFH】实时聊天室之WebSocket实战

The detailed process of building discuz forum is easy to understand

Leetcode102-二叉树的层序遍历详解

Android系统安全 — 5.3-APK V2签名介绍

Using OpenCV to do a simple face recognition

【汇编语言实战】一元二次方程ax2+bx+c=0求解(含源码与过程截屏,可修改参数)

Typora提示【This beta version of Typora is expired, please download and install a newer version】的解决方案

1、 Midwey addition, deletion, modification and query

Route interceptor

How to configure env.js in multiple environments in uni app
随机推荐
Houdini official HDA sidefx labs installation
[emotion] what is "excellent"
Read write lock, shared lock, exclusive lock
Seven data show the impact of tiktok's combination of payment and organic content
Opencv Chinese document 4.0.0 learning notes (updating...)
OpenCV中文文档4.0.0学习笔记(更新中……)
Typescript -- Generic
[FFH] websocket practice of real-time chat room
[Shangshui Shuo series together] June summary +no anxiety +july plan + how to test + how to improve
4、 Midway integrates swagger and supports JWT bearers
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,
Online lover
FreeRTOS - use of software timer
网络情人
Unity C tool class arrayhelper
JUC powerful auxiliary class
【我的创作一周年纪念日】爱情是需要被纪念的,创作也是
Treap
Notify consumers after provider information changes in RPC
Scatter chart - date