当前位置:网站首页>[binary tree] - middle order traversal of binary tree
[binary tree] - middle order traversal of binary tree
2022-06-24 06:53:00 【A baldhead who knocks code】
List of articles
Binary tree
Order traversal in binary tree
Middle order traversal idea
Official understanding :
First of all, we need to know what is the middle order traversal of binary tree : Follow to visit the left subtree —— The root node —— The right subtree traverses the tree , When we visit the left subtree or the right subtree, we traverse in the same way , Until you walk through the whole tree
author :LeetCode-Solution
link :https://leetcode.cn/problems/binary-tree-inorder-traversal/solution/er-cha-shu-de-zhong-xu-bian-li-by-leetcode-solutio/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
Personal understanding :
First, judge whether the binary tree is empty , If it is empty , Then return the empty string
If the binary tree is not empty ( loop / recursive )
First access the root node , Then access the left child node of the root node , Access the left child node of the left child node , Until a node has no left child node , This node is output
Determine whether the node has a right child node , If there is , Is to 1 operation , without , Go back to the root node of this node
Then access the root node , Output the root node
Then access the right child node of the root node , If the right child node exists , Then repeat 1, without , Then return to the root node of the root node , Conduct 1 operation
The illustration

- Judge that the binary tree is not empty
- visit A, visit A The left child of B, visit B The left child of D, visit D The left child of G, visit G The left child of , here G The left child node of does not exist , So the output G
- look for G Right child of ,G The right child of does not exist
- look for G The root node D, Output D
- Revisit D Right child of H,H The left child node of does not exist , So the output H
- look for H Right child of ,H The right child of does not exist , So back to D node
- look for D The root node of the node B, Output B
- B The right child node of the node does not exist , So back to B
- look for B The root node A, Output A, look for A Right child of C
- look for C The left child of E, look for E The left child of , There is no such thing as E The left child of , So the output E
- look for E Right child of I, look for I The left child of , non-existent , So the output I
- look for I Right child of , non-existent , So back to E node
- look for E The root node of the node C, Output C
- look for C Right child of node F, look for F The left child of , The left child node does not exist , So the output F
- look for F Right child of node , non-existent , So back to C, At this point, the tree traversal is completed
Finally, the middle order traversal of the graph is output as
| G | D | H | B | A | E | I | C | F |
|---|
Code implementation
- Ideas 1: Use recursion to implement
/** * The time and space utilization of this method are O(n) * * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
// establish list Save results
List<Integer> result = new ArrayList<Integer>();
// Connect the root node to result Pass in , Do middle order traversal
access(root, result);
return result;
}
public void access(TreeNode root, List result){
// Determine if the two tree is empty
if(root == null){
return ;
}
// Find the left child node
access(root.left, result);
// Output root node ( Here, the node without left and right child nodes can be regarded as the root node output )
result.add(root.val);
// Find the right child node
access(root.right,result);
}
}
- Train of thought two : Cyclic iteration method
public List<Integer> inorderTraversal(TreeNode root){
// establish list Save results
List<Integer> result = new ArrayList<Integer>();
// establish stack Stack to save the pressing order
Stack<TreeNode> stack = new Stack();
// If root Not empty , Will continue to be pushed into the stack , If root It's empty , The node will pop up from the stack
// there root It is changing. , Don't interpret it as the root node of the entire tree , It is the root node of each subtree
while(root != null || !stack.isEmpty()){
// When the current node is not empty , Push in the left child node of this node , Until the left child node does not exist
while(root != null){
stack.push(root);
root = root.left;
}
// Pop up the left child node just pressed
root = stack.pop();
// Output the result
result.add(root.val);
// Consider whether the right child node of this node is empty , If it is empty , Continue to pop up , If it's not empty , The right child node is regarded as a right subtree , Repeat the above operation
root = root.right;
}
return result;
}
边栏推荐
- Tencent Security jointly established a data security committee
- Domain name purchase method good domain name selection principle
- 项目Demo
- Innovating the security service mode, deeply convinced that the organization has been equipped with a "continuous online expert group"
- Word cannot copy and paste processing method
- leetcode:84. 柱状图中最大的矩形
- Analysis and treatment of easydss flash back caused by system time
- 【二叉树】——二叉树中序遍历
- leetcode:1856. 子数组最小乘积的最大值
- What are the audio formats? Can the audio format be converted
猜你喜欢

puzzle(019.1)Hook、Gear
![Command ‘[‘where‘, ‘cl‘]‘ returned non-zero exit status 1.](/img/2c/d04f5dfbacb62de9cf673359791aa9.png)
Command ‘[‘where‘, ‘cl‘]‘ returned non-zero exit status 1.

缓存操作rockscache原理图

35岁危机?内卷成程序员代名词了

Manual for automatic testing and learning of anti stepping pits, one for each tester

解读AI机器人产业发展的顶层设计

Rockscache schematic diagram of cache operation

C语言学生管理系统——可检查用户输入合法性,双向带头循环链表

leetcode:1856. Maximum value of minimum product of subarray

基于三维GIS系统的智慧水库管理应用
随机推荐
WordPress pill applet build applet from zero to one [applet registration configuration]
Forbid viewing source code in web page (protect source code)
Rockscache schematic diagram of cache operation
Produce kubeconfig with permission control
leetcode:84. The largest rectangle in the histogram
Command ‘[‘where‘, ‘cl‘]‘ returned non-zero exit status 1.
How does easyplayer RTSP configure sending heartbeat information to the server?
What are the easy-to-use character recognition software? Which are the mobile terminal and PC terminal respectively
puzzle(019.1)Hook、Gear
Free and easy-to-use screen recording and video cutting tool sharing
What is the role of website domain name
leetcode:1856. Maximum value of minimum product of subarray
目标5000万日活,Pwnk欲打造下一代年轻人的“迪士尼乐园”
Distributed cache breakdown
Surveying and mapping principle of GIS coordinate system: geoid / datum / reference ellipsoid /epsg/sri/wkt
DHCP server setup
Actual combat | how to deploy flask project using wechat cloud hosting
Innovating the security service mode, deeply convinced that the organization has been equipped with a "continuous online expert group"
Deploy DNS server using dnsmasq
Station B collapsed. Let's talk to the injured programmers