当前位置:网站首页>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
};边栏推荐
- Introduction to ffmpeg Lib
- Bat Android Engineer interview process analysis + restore the most authentic and complete first-line company interview questions
- Ubuntu20.04 PostgreSQL 14 installation configuration record
- 剑指 Offer 42. 连续子数组的最大和
- [Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
- Construction and maintenance of business websites [13]
- What are the necessary things for students to start school? Ranking list of Bluetooth headsets with good sound quality
- Three core problems of concurrent programming
- Golang lock
- 并发编程的三大核心问题
猜你喜欢

Discussion on the idea of platform construction

Implementation principle of city selector component

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

New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port

现货黄金分析的技巧有什么呢?

人工智能在网络安全中的作用

SQLite 3 of embedded database

MySQL view concept, create view, view, modify view, delete view

机器学习基本概念
![[question] - why is optical flow not good for static scenes](/img/8d/2cf6f582bc58cc2985f50e3f85f334.jpg)
[question] - why is optical flow not good for static scenes
随机推荐
734. Energy stone (greed, backpack)
三分钟学会基础k线图知识
医药管理系统(大一下C语言课设)
leetcode2311. 小于等于 K 的最长二进制子序列(中等,周赛)
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
This is the form of the K-line diagram (pithy formula)
How to solve MySQL master-slave delay problem
Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
The concept, function, characteristics, creation and deletion of MySQL constraints
Volume compression, decompression
Niuke - Huawei question bank (51~60)
Medical management system (C language course for freshmen)
Four basic strategies for migrating cloud computing workloads
剑指 Offer 62. 圆圈中最后剩下的数字
Introduction to ffmpeg Lib
This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable
Construction and maintenance of business websites [13]
How to build and use redis environment
Six lessons to be learned for the successful implementation of edge coding
Matlab uses resample to complete resampling