Skip to content

Commit 64db56d

Browse files
refactor 145
1 parent e8dfa71 commit 64db56d

File tree

1 file changed

+39
-28
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+39
-28
lines changed

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

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

10-
/**Given a binary tree, return the postorder traversal of its nodes' values.
10+
/**
11+
* 145. Binary Tree Postorder Traversal
12+
13+
Given a binary tree, return the postorder traversal of its nodes' values.
1114
1215
For example:
1316
Given binary tree {1,#,2,3},
@@ -21,40 +24,48 @@
2124
Note: Recursive solution is trivial, could you do it iteratively?*/
2225

2326
public class _145 {
24-
/**A tricky one: Modify the code for pre-order traversal so that it becomes root->right->left, and then reverse the result to get left->right->root.*/
25-
public static List<Integer> postorderTraversal_iterative(TreeNode root) {
26-
List<Integer> result = new ArrayList();
27-
if (root == null) {
28-
return result;
27+
public static class Solution1 {
28+
/**
29+
* A tricky one: Modify the code for pre-order traversal
30+
* so that it becomes root->right->left,
31+
* and then reverse the result to get left->right->root.
32+
*/
33+
public static List<Integer> postorderTraversal(TreeNode root) {
34+
List<Integer> result = new ArrayList();
35+
if (root == null) {
36+
return result;
37+
}
38+
Stack<TreeNode> stack = new Stack();
39+
stack.push(root);
40+
while (!stack.isEmpty()) {
41+
root = stack.pop();
42+
result.add(root.val);
43+
if (root.left != null) {
44+
stack.push(root.left);
2945
}
30-
Stack<TreeNode> stack = new Stack();
31-
stack.push(root);
32-
while (!stack.isEmpty()) {
33-
root = stack.pop();
34-
result.add(root.val);
35-
if (root.left != null) {
36-
stack.push(root.left);
37-
}
38-
if (root.right != null) {
39-
stack.push(root.right);
40-
}
46+
if (root.right != null) {
47+
stack.push(root.right);
4148
}
42-
Collections.reverse(result);
43-
return result;
49+
}
50+
Collections.reverse(result);
51+
return result;
4452
}
53+
}
4554

46-
public List<Integer> postorderTraversal_recursive(TreeNode root) {
47-
List<Integer> result = new ArrayList();
48-
return post(root, result);
55+
public static class Solution2 {
56+
public List<Integer> postorderTraversal(TreeNode root) {
57+
List<Integer> result = new ArrayList();
58+
return post(root, result);
4959
}
5060

5161
List<Integer> post(TreeNode root, List<Integer> result) {
52-
if (root == null) {
53-
return result;
54-
}
55-
post(root.left, result);
56-
post(root.right, result);
57-
result.add(root.val);
62+
if (root == null) {
5863
return result;
64+
}
65+
post(root.left, result);
66+
post(root.right, result);
67+
result.add(root.val);
68+
return result;
5969
}
70+
}
6071
}

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