-
Notifications
You must be signed in to change notification settings - Fork 0
/
145.二叉树的后序遍历.js
74 lines (70 loc) · 1.58 KB
/
145.二叉树的后序遍历.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* @lc app=leetcode.cn id=145 lang=javascript
*
* [145] 二叉树的后序遍历
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var postorderTraversal = function (root) {
// 递归方式
// if (root === null) return []
// const res = []
// if (root.left) {
// res.push(...postorderTraversal(root.left))
// }
// if (root.right) {
// res.push(...postorderTraversal(root.right))
// }
// res.push(root.val)
// return res
// 迭代方式
if (root === null) {
return []
}
// 记录结果
const res = []
// node 栈
let stack = []
// 下一步操作:0 压入左子树, 1 压入右子树, 2 弹出栈
let statusStack = []
stack.push(root)
statusStack.push(0)
while (stack.length !== 0) {
const topStatus = statusStack.pop()
const top = stack[stack.length - 1]
switch (topStatus) {
case 0:
statusStack.push(1)
if (top.left !== null) {
stack.push(top.left)
statusStack.push(0)
}
break
case 1:
statusStack.push(2)
if (top.right !== null) {
stack.push(top.right)
statusStack.push(0)
}
break
case 2:
// statusStack.pop()
const topNode = stack.pop()
res.push(topNode.val)
break
}
}
return res
}
// @lc code=end