-
Notifications
You must be signed in to change notification settings - Fork 0
/
145.二叉树的后序遍历.js
93 lines (89 loc) · 2 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* @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) {
/**
* 递归
*
let result = [];
function traverse(root) {
if (!root) return [];
traverse(root.left);
traverse(root.right);
result.push(root.val);
}
traverse(root);
return result; */
/**
* 迭代
*
*/
if (!root) return [];
const result = [];
const stack = [root];
// 后序遍历是父节点需要最后被遍历。但其实可以跟前序遍历的实现方式上差不多,
// 只不过在插入数组中,我们总是在头部插入,这样先被插入的节点值一定是相对于左右孩子后面的。
// 后序:left->right->root, 所以按照unshift,顺序应该是root->left->right
while (stack.length > 0) {
const node = stack.pop();
result.unshift(node.val);
if (node.left) {
stack.push(node.left);
}
if (node.right) {
stack.push(node.right);
}
}
return result;
/**
* 迭代,带有visited标志位
*
const result = [];
const stack = [];
if (!root) return [];
stack.push({
node: root,
visited: false,
});
while (stack.length > 0) {
const { node, visited } = stack.pop();
if (visited) {
result.push(node.val);
} else {
// left->right->root => stack: root->right->left
stack.push({
node,
visited: true,
});
if (node.right) {
stack.push({
node: node.right,
visited: false,
});
}
if (node.left) {
stack.push({
node: node.left,
visited: false,
});
}
}
}
return result */
};
// @lc code=end