Skip to content

Commit ee7fa22

Browse files
refactor 145
1 parent 667fea8 commit ee7fa22

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.ArrayList;
66
import java.util.Collections;
7+
import java.util.LinkedList;
78
import java.util.List;
89
import java.util.Stack;
910

@@ -30,7 +31,7 @@ public static class Solution1 {
3031
* so that it becomes root->right->left,
3132
* and then reverse the result to get left->right->root.
3233
*/
33-
public static List<Integer> postorderTraversal(TreeNode root) {
34+
public List<Integer> postorderTraversal(TreeNode root) {
3435
List<Integer> result = new ArrayList();
3536
if (root == null) {
3637
return result;
@@ -53,6 +54,35 @@ public static List<Integer> postorderTraversal(TreeNode root) {
5354
}
5455

5556
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);
76+
}
77+
if (curr.right != null) {
78+
stack.push(curr.right);
79+
}
80+
}
81+
return list;
82+
}
83+
}
84+
85+
public static class Solution3 {
5686
public List<Integer> postorderTraversal(TreeNode root) {
5787
List<Integer> result = new ArrayList();
5888
return post(root, result);

src/test/java/com/fishercoder/_145Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
public class _145Test {
1717
private static _145.Solution1 solution1;
1818
private static _145.Solution2 solution2;
19+
private static _145.Solution3 solution3;
1920
private static TreeNode root;
2021
private static List<Integer> expected;
2122

2223
@BeforeClass
2324
public static void setup() {
2425
solution1 = new _145.Solution1();
2526
solution2 = new _145.Solution2();
27+
solution3 = new _145.Solution3();
2628
}
2729

2830
@Test
@@ -31,8 +33,10 @@ public void test1() {
3133
TreeUtils.printBinaryTree(root);
3234
CommonUtils.printList(solution1.postorderTraversal(root));
3335
CommonUtils.printList(solution2.postorderTraversal(root));
36+
CommonUtils.printList(solution3.postorderTraversal(root));
3437
expected = new ArrayList<>(Arrays.asList(8, 9, 7, 4, 2, 5, 6, 3, 1));
3538
assertEquals(expected, solution1.postorderTraversal(root));
3639
assertEquals(expected, solution2.postorderTraversal(root));
40+
assertEquals(expected, solution3.postorderTraversal(root));
3741
}
3842
}

0 commit comments

Comments
 (0)