Skip to content

Commit f442f51

Browse files
refactor 144
1 parent df7c3b1 commit f442f51

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.ArrayList;
77
import java.util.Deque;
88
import java.util.List;
9+
import java.util.Stack;
910

1011
/**
1112
* 144. Binary Tree Preorder Traversal
@@ -26,21 +27,14 @@
2627
public class _144 {
2728
public static class Solution1 {
2829
public List<Integer> preorderTraversal(TreeNode root) {
29-
List<Integer> list = new ArrayList();
30-
if (root == null) {
31-
return list;
32-
}
33-
Deque<TreeNode> stack = new ArrayDeque<>();
30+
List<Integer> list = new ArrayList<>();
31+
Stack<TreeNode> stack = new Stack<>();
3432
stack.push(root);
3533
while (!stack.isEmpty()) {
3634
TreeNode curr = stack.pop();
37-
list.add(curr.val);
38-
/**We push right nodes onto the stack first, since they'll be popped out later than
39-
* the left nodes, to meet the preorder: root -> left -> right. */
40-
if (curr.right != null) {
35+
if (curr != null) {
36+
list.add(curr.val);
4137
stack.push(curr.right);
42-
}
43-
if (curr.left != null) {
4438
stack.push(curr.left);
4539
}
4640
}

src/test/java/com/fishercoder/_144Test.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.fishercoder;
22

33
import com.fishercoder.common.classes.TreeNode;
4-
import com.fishercoder.common.utils.CommonUtils;
54
import com.fishercoder.common.utils.TreeUtils;
65
import com.fishercoder.solutions._144;
76
import org.junit.BeforeClass;
@@ -10,6 +9,8 @@
109
import java.util.Arrays;
1110
import java.util.List;
1211

12+
import static org.junit.Assert.assertEquals;
13+
1314
public class _144Test {
1415
private static _144.Solution1 solution1;
1516
private static _144.Solution2 solution2;
@@ -26,15 +27,30 @@ public static void setup() {
2627
public void test1() {
2728
root = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4));
2829
inorder = solution1.preorderTraversal(root);
29-
CommonUtils.printList(inorder);
30+
assertEquals(Arrays.asList(3, 1, 5, 2, 4), inorder);
3031
}
3132

3233
@Test
3334
public void test2() {
3435
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, 6, null, 7, null, null, null, null, 8, 9));
3536
TreeUtils.printBinaryTree(root);
3637
inorder = solution1.preorderTraversal(root);
37-
CommonUtils.printList(inorder);
38+
assertEquals(Arrays.asList(1, 2, 4, 7, 8, 9, 3, 5, 6), inorder);
39+
}
40+
41+
@Test
42+
public void test3() {
43+
root = TreeUtils.constructBinaryTree(Arrays.asList(3, 1, null, null, 5, 2, null, null, 4));
44+
inorder = solution2.preorderTraversal(root);
45+
assertEquals(Arrays.asList(3, 1, 5, 2, 4), inorder);
46+
}
47+
48+
@Test
49+
public void test4() {
50+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, null, 5, 6, null, 7, null, null, null, null, 8, 9));
51+
TreeUtils.printBinaryTree(root);
52+
inorder = solution2.preorderTraversal(root);
53+
assertEquals(Arrays.asList(1, 2, 4, 7, 8, 9, 3, 5, 6), inorder);
3854
}
3955

4056
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy