当前位置:网站首页>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
};边栏推荐
- Design and implementation of key value storage engine based on LSM tree
- Deep learning: a solution to over fitting in deep neural networks
- 剑指 Offer II 031. 最近最少使用缓存
- Three core problems of concurrent programming
- 2022 Q2 - Summary of skills to improve skills
- 【视频】马尔可夫链蒙特卡罗方法MCMC原理与R语言实现|数据分享
- [技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
- Ubuntu20.04 PostgreSQL 14 installation configuration record
- np.where 和 torch.where 用法
- New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
猜你喜欢

【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享

Matlab uses audioread and sound to read and play WAV files

MySQL如何解决delete大量数据后空间不释放的问题

开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?

Medical management system (C language course for freshmen)

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/56/87bc8fca9ceeab6484f567f7231fdb.png)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing

734. Energy stone (greed, backpack)

Data analysis on the disaster of Titanic

The smart Park "ZhongGuanCun No.1" subverts your understanding of the park
随机推荐
MySQL约束与多表查询实例分析
正则表达式学习笔记
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
Based on configured schedule, the given trigger will never fire
Selection of field types for creating tables in MySQL database
leetcode2309. 兼具大小写的最好英文字母(简单,周赛)
2022 Q2 - résumé des compétences pour améliorer les compétences
【LeetCode 43】236. The nearest common ancestor of binary tree
MySQL主从延迟问题怎么解决
new和malloc的区别
电子协会 C语言 1级 33 、奇偶数判断
C language 3-7 daffodils (enhanced version)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
What are the skills of spot gold analysis?
Medical management system (C language course for freshmen)
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
TSINGSEE青犀平台如何实现同一节点同时播放多个视频?
MPLS experiment operation
321. Chessboard segmentation (2D interval DP)
5g/4g pole gateway_ Smart pole gateway