Skip to content

Commit e8dfa71

Browse files
refactor 144
1 parent ecaa664 commit e8dfa71

File tree

1 file changed

+33
-31
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+33
-31
lines changed

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

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import java.util.Deque;
88
import java.util.List;
99

10-
1110
/**
1211
* 144. Binary Tree Preorder Traversal
13-
* Given a binary tree, return the preorder traversal of its nodes' values.
12+
13+
Given a binary tree, return the preorder traversal of its nodes' values.
1414
1515
For example:
1616
Given binary tree {1,#,2,3},
@@ -24,42 +24,44 @@
2424
Note: Recursive solution is trivial, could you do it iteratively?*/
2525

2626
public class _144 {
27-
28-
public List<Integer> preorderTraversal_iterative(TreeNode root) {
29-
List<Integer> list = new ArrayList();
30-
if (root == null) {
31-
return list;
27+
public static class Solution1 {
28+
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<>();
34+
stack.push(root);
35+
while (!stack.isEmpty()) {
36+
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) {
41+
stack.push(curr.right);
3242
}
33-
Deque<TreeNode> stack = new ArrayDeque<>();
34-
stack.push(root);
35-
while (!stack.isEmpty()) {
36-
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) {
41-
stack.push(curr.right);
42-
}
43-
if (curr.left != null) {
44-
stack.push(curr.left);
45-
}
43+
if (curr.left != null) {
44+
stack.push(curr.left);
4645
}
47-
return list;
46+
}
47+
return list;
4848
}
49+
}
4950

50-
public List<Integer> preorderTraversal_recursive(TreeNode root) {
51-
List<Integer> list = new ArrayList();
52-
return pre(root, list);
51+
public static class Solution2 {
52+
public List<Integer> preorderTraversal(TreeNode root) {
53+
List<Integer> list = new ArrayList();
54+
return pre(root, list);
5355
}
5456

5557
List<Integer> pre(TreeNode root, List<Integer> list) {
56-
if (root == null) {
57-
return list;
58-
}
59-
list.add(root.val);
60-
pre(root.left, list);
61-
pre(root.right, list);
58+
if (root == null) {
6259
return list;
60+
}
61+
list.add(root.val);
62+
pre(root.left, list);
63+
pre(root.right, list);
64+
return list;
6365
}
64-
66+
}
6567
}

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