当前位置:网站首页>Iterative unified writing method of binary tree
Iterative unified writing method of binary tree
2022-07-02 01:57:00 【Front end pww】
Put the accessed node on the stack , Put the node to be processed in the stack, but mark it .
How to mark , After the nodes to be processed are put on the stack , Then put a null pointer as a marker .
Before the order :
var preorderTraversal = function(root) {
let s=[root]
let arr=[]
if(root===null){
return arr
}
while(s.length){
// Take out the head node
let node=s.pop()
// If the current node is a marked node
if(node==null){
// Put the next node ( root ) Save to array
arr.push(s.pop().val)
continue
}
// Right
node.right&&s.push(node.right)
// Left
node.left&&s.push(node.left)
// root
s.push(node)
// Mark
s.push(null)
}
return arr
};
In the following order :
var postorderTraversal = function(root) {
let s=[root]
let arr=[]
if(root===null){
return arr
}
while(s.length){
let node=s.pop()
if(node===null){
arr.push(s.pop().val)
continue
}
// root
s.push(node)
// Mark
s.push(null)
// Right
node.right&&s.push(node.right)
// Left
node.left&&s.push(node.left)
}
return arr
};
Middle preface :
var inorderTraversal = function(root) {
let s=[root]
let arr=[]
if(root===null){
return arr
}
while(s.length){
let node=s.pop()
if(node===null){
arr.push(s.pop().val)
continue
}
// Right
node.right&&s.push(node.right)
// root
s.push(node)
s.push(null)
// Left
node.left&&s.push(node.left)
}
return arr
};
边栏推荐
- leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
- Software No.1
- leetcode2312. 卖木头块(困难,周赛)
- Discussion on the idea of platform construction
- Quatre stratégies de base pour migrer la charge de travail de l'informatique en nuage
- MPLS experiment operation
- What is AQS and its principle
- Learn basic K-line diagram knowledge in three minutes
- The difference between new and malloc
- Regular expression learning notes
猜你喜欢
leetcode2310. The one digit number is the sum of integers of K (medium, weekly)
MySQL view concept, create view, view, modify view, delete view
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
Cross domain? Homology? Understand what is cross domain at once
321. Chessboard segmentation (2D interval DP)
JMeter (II) - install the custom thread groups plug-in
The role of artificial intelligence in network security
Raspberry pie 4B learning notes - IO communication (1-wire)
leetcode2312. 卖木头块(困难,周赛)
Discussion on the idea of platform construction
随机推荐
JMeter (I) - download, installation and plug-in management
[C #] use regular verification content
leetcode2311. Longest binary subsequence less than or equal to K (medium, weekly)
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
MySQL中一条SQL是怎么执行的
Design and implementation of key value storage engine based on LSM tree
Five skills of adding audio codec to embedded system
Experimental reproduction of variable image compression with a scale hyperprior
There are spaces in the for loop variable in the shell -- IFS variable
Construction and maintenance of business websites [13]
PR second training
[Maya] the error of importing Maya into Metahuman
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
Redis有序集合如何使用
人工智能在网络安全中的作用
How to build and use redis environment
Feature extraction and detection 16 brisk feature detection and matching
Redis环境搭建和使用的方法
Electronic Association C language level 1 33, odd even number judgment