|
7 | 7 | import java.util.List; |
8 | 8 | import java.util.Stack; |
9 | 9 |
|
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. |
11 | 14 |
|
12 | 15 | For example: |
13 | 16 | Given binary tree {1,#,2,3}, |
|
21 | 24 | Note: Recursive solution is trivial, could you do it iteratively?*/ |
22 | 25 |
|
23 | 26 | 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); |
29 | 45 | } |
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); |
41 | 48 | } |
42 | | - Collections.reverse(result); |
43 | | - return result; |
| 49 | + } |
| 50 | + Collections.reverse(result); |
| 51 | + return result; |
44 | 52 | } |
| 53 | + } |
45 | 54 |
|
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); |
49 | 59 | } |
50 | 60 |
|
51 | 61 | 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) { |
58 | 63 | return result; |
| 64 | + } |
| 65 | + post(root.left, result); |
| 66 | + post(root.right, result); |
| 67 | + result.add(root.val); |
| 68 | + return result; |
59 | 69 | } |
| 70 | + } |
60 | 71 | } |
0 commit comments