当前位置:网站首页>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
};边栏推荐
- 分卷压缩,解压
- 并发编程的三大核心问题
- 【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
- 734. Energy stone (greed, backpack)
- [Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
- Software No.1
- Experimental reproduction of variable image compression with a scale hyperprior
- [question] - why is optical flow not good for static scenes
- This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
- How to use a product to promote "brand thrill"?
猜你喜欢
随机推荐
matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
Basic concepts of machine learning
leetcode373. 查找和最小的 K 对数字(中等)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
321. Chessboard segmentation (2D interval DP)
Word search applet design report based on cloud development +ppt+ project source code + demonstration video
Golang lock
Makefile simple induction
np. Where and torch Where usage
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
剑指 Offer 47. 礼物的最大价值
跨域?同源?一次搞懂什么是跨域
剑指 Offer 42. 连续子数组的最大和
The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
Construction and maintenance of business websites [15]
D discard the virtual recovery method
2022 Q2 - Summary of skills to improve skills
479. Additive binary tree (interval DP on the tree)
Redis有序集合如何使用
leetcode2312. 卖木头块(困难,周赛)









