-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy path145.binary-tree-postorder-traversal.java
65 lines (64 loc) · 1.31 KB
/
145.binary-tree-postorder-traversal.java
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
/*
* @lc app=leetcode id=145 lang=java
*
* [145] Binary Tree Postorder Traversal
*
* https://leetcode.com/problems/binary-tree-postorder-traversal/description/
*
* algorithms
* Hard (46.93%)
* Total Accepted: 245.5K
* Total Submissions: 516.7K
* Testcase Example: '[1,null,2,3]'
*
* 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.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
TreeNode last = null;
while (cur != null || !stack.empty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.peek();
if (cur.right == null || cur.right == last) {
list.add(cur.val);
stack.pop();
last = cur;
cur = null;
} else {
cur = cur.right;
}
}
return list;
}
}