# 二叉树的遍历
# 前序遍历
function prePrint(root){
if(root){
console.log(root.val)
prePrint(root.left)
prePrint(root.right)
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 中序遍历
function prePrint(root){
if(root){
prePrint(root.left)
console.log(root.val)
prePrint(root.right)
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 后序遍历
function prePrint(root){
if(root){
prePrint(root.left)
prePrint(root.right)
console.log(root.val)
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 层序遍历
function sequenceTraversal(root){
if (!this.root) {
return
}
let q = []
q.push(this.root)
while (q.length>0) {
let t = q.shift()
console.log(t.value)
if (t.left) q.push(t.left)
if (t.right) q.push(t.right)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13