Skip to content

Commit 38bdb78

Browse files
refactor 144
1 parent e787afd commit 38bdb78

File tree

1 file changed

+42
-58
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+42
-58
lines changed

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

+42-58
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,55 @@
88
import java.util.List;
99
import java.util.Stack;
1010

11-
/**
12-
* 144. Binary Tree Preorder Traversal
13-
14-
Given a binary tree, return the preorder 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 [1,2,3].
24-
25-
Note: Recursive solution is trivial, could you do it iteratively?*/
26-
2711
public class _144 {
28-
public static class Solution1 {
29-
public List<Integer> preorderTraversal(TreeNode root) {
30-
List<Integer> list = new ArrayList<>();
31-
Stack<TreeNode> stack = new Stack<>();
32-
stack.push(root);
33-
while (!stack.isEmpty()) {
34-
TreeNode curr = stack.pop();
35-
if (curr != null) {
36-
list.add(curr.val);
37-
stack.push(curr.right);
38-
stack.push(curr.left);
12+
public static class Solution1 {
13+
public List<Integer> preorderTraversal(TreeNode root) {
14+
List<Integer> list = new ArrayList<>();
15+
Stack<TreeNode> stack = new Stack<>();
16+
stack.push(root);
17+
while (!stack.isEmpty()) {
18+
TreeNode curr = stack.pop();
19+
if (curr != null) {
20+
list.add(curr.val);
21+
stack.push(curr.right);
22+
stack.push(curr.left);
23+
}
24+
}
25+
return list;
3926
}
40-
}
41-
return list;
4227
}
43-
}
4428

45-
public static class Solution2 {
46-
public List<Integer> preorderTraversal(TreeNode root) {
47-
List<Integer> list = new ArrayList();
48-
return pre(root, list);
49-
}
29+
public static class Solution2 {
30+
public List<Integer> preorderTraversal(TreeNode root) {
31+
List<Integer> list = new ArrayList();
32+
return pre(root, list);
33+
}
5034

51-
List<Integer> pre(TreeNode root, List<Integer> list) {
52-
if (root == null) {
53-
return list;
54-
}
55-
list.add(root.val);
56-
pre(root.left, list);
57-
pre(root.right, list);
58-
return list;
35+
List<Integer> pre(TreeNode root, List<Integer> list) {
36+
if (root == null) {
37+
return list;
38+
}
39+
list.add(root.val);
40+
pre(root.left, list);
41+
pre(root.right, list);
42+
return list;
43+
}
5944
}
60-
}
6145

62-
public static class Solution3 {
63-
public List<Integer> preorderTraversal(TreeNode root) {
64-
List<Integer> list = new ArrayList<>();
65-
Stack<TreeNode> stack = new Stack<>();
66-
while (!stack.isEmpty() || root != null) {
67-
while (root != null) {
68-
list.add(root.val);
69-
stack.push(root);
70-
root = root.left;
46+
public static class Solution3 {
47+
public List<Integer> preorderTraversal(TreeNode root) {
48+
List<Integer> list = new ArrayList<>();
49+
Stack<TreeNode> stack = new Stack<>();
50+
while (!stack.isEmpty() || root != null) {
51+
while (root != null) {
52+
list.add(root.val);
53+
stack.push(root);
54+
root = root.left;
55+
}
56+
root = stack.pop();
57+
root = root.right;
58+
}
59+
return list;
7160
}
72-
root = stack.pop();
73-
root = root.right;
74-
}
75-
return list;
7661
}
77-
}
7862
}

0 commit comments

Comments
 (0)