当前位置:网站首页>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
};边栏推荐
- MySQL如何解决delete大量数据后空间不释放的问题
- 跨域?同源?一次搞懂什么是跨域
- The smart Park "ZhongGuanCun No.1" subverts your understanding of the park
- Experimental reproduction of variable image compression with a scale hyperprior
- Feature extraction and detection 16 brisk feature detection and matching
- 2022 Q2 - 提升技能的技巧总结
- 机器学习基本概念
- 城市选择器组件实现原理
- Based on configured schedule, the given trigger will never fire
- leetcode2310. The one digit number is the sum of integers of K (medium, weekly)
猜你喜欢
随机推荐
[question] - why is optical flow not good for static scenes
MySQL约束与多表查询实例分析
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
"C language programming", 4th Edition, edited by he Qinming and Yan Hui, after class exercise answers Chapter 3 branch structure
三分钟学会基础k线图知识
matlab 使用 audioread 、 sound 读取和播放 wav 文件
城市选择器组件实现原理
剑指 Offer 29. 顺时针打印矩阵
剑指 Offer 31. 栈的压入、弹出序列
2022 Q2 - 提升技能的技巧总结
Implementation principle of city selector component
SAP ui5 beginner tutorial 20 - explanation of expression binding usage of SAP ui5
leetcode2309. The best English letters with both upper and lower case (simple, weekly)
VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
* and & symbols in C language
Android: the kotlin language uses grendao3, a cross platform app development framework
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
321. Chessboard segmentation (2D interval DP)
leetcode373. Find and minimum k-pair numbers (medium)









