当前位置:网站首页>[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;
}
边栏推荐
- Programmers use personalized Wallpapers
- How to build a website after having a domain name? Can you ask others to help register the domain name
- 十年
- Coding platform project construction guide
- How accurate are the two common methods of domain name IP query
- Easynvr is optimized when a large number of videos are not online or unstable due to streaming failure
- Koa source code analysis
- sql join的使用
- Nine unique skills of Huawei cloud low latency Technology
- Project demo
猜你喜欢

About Stacked Generalization

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

leetcode:85. 最大矩形

leetcode:85. Max rectangle

Record -- about the method of adding report control to virtual studio2017 -- reportview control

oracle sql综合运用 习题

缓存操作rockscache原理图

【JUC系列】Executor框架之CompletionFuture

基于三维GIS系统的智慧水库管理应用

Rockscache schematic diagram of cache operation
随机推荐
About Stacked Generalization
With a goal of 50million days' living, pwnk wants to build a "Disneyland" for the next generation of young people
How does go limit the flow of services?
Cloud native high availability and Disaster Recovery Series (I): pod break up scheduling
基于三维GIS系统的智慧水库管理应用
What is domain name resolution? How to resolve domain name resolution errors
程序员使用个性壁纸
oracle sql综合运用 习题
How to choose CMS website system for website construction
Surveying and mapping principle of GIS coordinate system: geoid / datum / reference ellipsoid /epsg/sri/wkt
华为云低时延技术的九大绝招
Project demo
Station B collapsed. Let's talk to the injured programmers
On BOM and DOM (2): DOM node hierarchy / attributes / Selectors / node relationships / detailed operation
Tencent Security jointly established a data security committee
How to apply 5g smart pole to smart highway
Spirit information development log (1)
The product layout is strengthened, the transformation of digital intelligence is accelerated, and FAW Toyota has hit 2022million annual sales
What is the difference between level 1, level 2 and level 3 domain names? How to register domain names
How to build a website after having a domain name? Can you ask others to help register the domain name