Skip to content

Commit 6ab5600

Browse files
refactor 145
1 parent 38bdb78 commit 6ab5600

File tree

1 file changed

+69
-81
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+69
-81
lines changed

src/main/java/com/fishercoder/solutions/_145.java

+69-81
Original file line numberDiff line numberDiff line change
@@ -8,94 +8,82 @@
88
import java.util.List;
99
import java.util.Stack;
1010

11-
/**
12-
* 145. Binary Tree Postorder Traversal
13-
14-
Given a binary tree, return the postorder traversal of its nodes' values.
15-
16-
For example:
17-
Given binary tree {1,#,2,3},
18-
1
19-
\
20-
2
21-
/
22-
3
23-
return [3,2,1].
24-
25-
Note: Recursive solution is trivial, could you do it iteratively?*/
26-
2711
public class _145 {
28-
public static class Solution1 {
29-
/**
30-
* A tricky one: Modify the code for pre-order traversal
31-
* so that it becomes root->right->left,
32-
* and then reverse the result to get left->right->root.
33-
*/
34-
public List<Integer> postorderTraversal(TreeNode root) {
35-
List<Integer> result = new ArrayList();
36-
if (root == null) {
37-
return result;
38-
}
39-
Stack<TreeNode> stack = new Stack();
40-
stack.push(root);
41-
while (!stack.isEmpty()) {
42-
root = stack.pop();
43-
result.add(root.val);
44-
if (root.left != null) {
45-
stack.push(root.left);
46-
}
47-
if (root.right != null) {
48-
stack.push(root.right);
12+
public static class Solution1 {
13+
/**
14+
* A tricky/hacky one: Modify the code for pre-order traversal
15+
* so that it becomes root->right->left,
16+
* and then reverse the result to get left->right->root.
17+
*/
18+
public List<Integer> postorderTraversal(TreeNode root) {
19+
List<Integer> result = new ArrayList();
20+
if (root == null) {
21+
return result;
22+
}
23+
Stack<TreeNode> stack = new Stack();
24+
stack.push(root);
25+
while (!stack.isEmpty()) {
26+
root = stack.pop();
27+
result.add(root.val);
28+
if (root.left != null) {
29+
stack.push(root.left);
30+
}
31+
if (root.right != null) {
32+
stack.push(root.right);
33+
}
34+
}
35+
Collections.reverse(result);
36+
return result;
4937
}
50-
}
51-
Collections.reverse(result);
52-
return result;
5338
}
54-
}
5539

56-
public static class Solution2 {
57-
/**Or use a LinkedList and add values to the head, then no reverse is needed.
58-
* the linked list contents get added like this:
59-
*
60-
* root
61-
* right, root
62-
* left, right, root
63-
* */
64-
public List<Integer> postorderTraversal(TreeNode root) {
65-
List<Integer> list = new LinkedList<>();
66-
if (root == null) {
67-
return list;
68-
}
69-
Stack<TreeNode> stack = new Stack<>();
70-
stack.push(root);
71-
while (!stack.isEmpty()) {
72-
TreeNode curr = stack.pop();
73-
list.add(0, curr.val);
74-
if (curr.left != null) {
75-
stack.push(curr.left);
40+
public static class Solution2 {
41+
/**
42+
* Or use a LinkedList and add values to the head, then no reverse is needed.
43+
* the linked list contents get added like this:
44+
* <p>
45+
* root
46+
* right, root
47+
* left, right, root
48+
*/
49+
public List<Integer> postorderTraversal(TreeNode root) {
50+
List<Integer> list = new LinkedList<>();
51+
if (root == null) {
52+
return list;
53+
}
54+
Stack<TreeNode> stack = new Stack<>();
55+
stack.push(root);
56+
while (!stack.isEmpty()) {
57+
TreeNode curr = stack.pop();
58+
list.add(0, curr.val);
59+
if (curr.left != null) {
60+
stack.push(curr.left);
61+
}
62+
if (curr.right != null) {
63+
stack.push(curr.right);
64+
}
65+
}
66+
return list;
7667
}
77-
if (curr.right != null) {
78-
stack.push(curr.right);
79-
}
80-
}
81-
return list;
8268
}
83-
}
8469

85-
public static class Solution3 {
86-
public List<Integer> postorderTraversal(TreeNode root) {
87-
List<Integer> result = new ArrayList();
88-
return post(root, result);
89-
}
70+
public static class Solution3 {
71+
/**
72+
* recursive solution is trivial.
73+
*/
74+
public List<Integer> postorderTraversal(TreeNode root) {
75+
List<Integer> result = new ArrayList();
76+
return post(root, result);
77+
}
9078

91-
List<Integer> post(TreeNode root, List<Integer> result) {
92-
if (root == null) {
93-
return result;
94-
}
95-
post(root.left, result);
96-
post(root.right, result);
97-
result.add(root.val);
98-
return result;
79+
List<Integer> post(TreeNode root, List<Integer> result) {
80+
if (root == null) {
81+
return result;
82+
}
83+
post(root.left, result);
84+
post(root.right, result);
85+
result.add(root.val);
86+
return result;
87+
}
9988
}
100-
}
10189
}

0 commit comments

Comments
 (0)