-
Notifications
You must be signed in to change notification settings - Fork 1
/
145.binary_tree_postorder_traversal.js
56 lines (52 loc) · 1.43 KB
/
145.binary_tree_postorder_traversal.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
/*
* date: 2019-01-05
* author: Level.Z
* source: https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
* Given a binary tree, return the postorder traversal of its nodes' values.
* Example:
* ```
* Input: [1,null,2,3]
* 1
* \
* 2
* /
* 3
* Output: [3,2,1]
* ```
* Follow up: Recursive solution is trivial, could you do it iteratively?
*/
// Definition for a binary tree node.
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
/*
* 解法一:递归
*/
const postorderTraversal1 = function(root) {
if (!root) return [];
function _postorderTraversal(root, result = []) {
if (root.left) _postorderTraversal(root.left, result);
if (root.right) _postorderTraversal(root.right, result);
result.push(root.val);
return result;
}
return _postorderTraversal(root, []);
};
/*
* 解法二:迭代
* 因为后序遍历的顺序是左子节点->右子节点->节点,所以我们可以先将节点值放入result中,然后将左子节点和右子节点押入栈中,在从栈中分别弹出来插到result的头部完成遍历
*/
const postorderTraversal2 = function (root) {
let result = [];
if (!root) return result;
let stack = [ root ];
while(root || stack.length > 0) {
root = stack.pop();
if (!root) continue;
result.unshift(root.val);
if (root.left) stack.push(root.left);
if (root.right) stack.push(root.right);
}
return result;
}