|
12 | 12 |
|
13 | 13 | Note:
|
14 | 14 | You may assume that duplicates do not exist in the tree.
|
15 |
| -
|
16 | 15 | */
|
17 | 16 | public class _106 {
|
| 17 | + public static class Solution1 { |
18 | 18 |
|
19 |
| - /** |
20 |
| - * https://discuss.leetcode.com/topic/3296/my-recursive-java-code-with-o-n-time-and-o-n-space |
21 |
| - * |
22 |
| - * Note: the last element of postorder array is the root! |
23 |
| - * |
24 |
| - * The idea is to take the last element in postorder as the root; find the position of the root in the inorder array; |
25 |
| - * then locate the range for left sub-tree and right sub-tree and do recursion, |
26 |
| - * use a hashmap to record the index of root in the inorder array. |
27 |
| - */ |
28 |
| - public TreeNode buildTree(int[] inorder, int[] postorder) { |
29 |
| - if (inorder == null || postorder == null || inorder.length != postorder.length) { |
30 |
| - return null; |
31 |
| - } |
32 |
| - HashMap<Integer, Integer> inorderMap = new HashMap<>(); |
33 |
| - for (int i = 0; i < inorder.length; i++) { |
34 |
| - inorderMap.put(inorder[i], i); |
| 19 | + /** |
| 20 | + * https://discuss.leetcode.com/topic/3296/my-recursive-java-code-with-o-n-time-and-o-n-space |
| 21 | + * |
| 22 | + * Note: the last element of postorder array is the root! |
| 23 | + * |
| 24 | + * The idea is to take the last element in postorder as the root; find the position of the root |
| 25 | + * in the inorder array; then locate the range for left sub-tree and right sub-tree and do |
| 26 | + * recursion, use a hashmap to record the index of root in the inorder array. |
| 27 | + */ |
| 28 | + public TreeNode buildTree(int[] inorder, int[] postorder) { |
| 29 | + if (inorder == null || postorder == null || inorder.length != postorder.length) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + HashMap<Integer, Integer> inorderMap = new HashMap<>(); |
| 33 | + for (int i = 0; i < inorder.length; i++) { |
| 34 | + inorderMap.put(inorder[i], i); |
| 35 | + } |
| 36 | + /**At the beginning, both start from 0 to nums.length-1*/ |
| 37 | + return buildTreeRecursively(inorderMap, 0, inorder.length - 1, postorder, 0, |
| 38 | + postorder.length - 1); |
35 | 39 | }
|
36 |
| - /**At the beginning, both start from 0 to nums.length-1*/ |
37 |
| - return buildTreeRecursively(inorderMap, 0, inorder.length - 1, postorder, 0, postorder.length - 1); |
38 |
| - } |
39 | 40 |
|
40 |
| - private TreeNode buildTreeRecursively(Map<Integer, Integer> inorderMap, int inorderStart, int inorderEnd, int[] postorder, int postorderStart, int postorderEnd) { |
41 |
| - if (postorderStart > postorderEnd || inorderStart > inorderEnd) { |
42 |
| - return null; |
43 |
| - } |
44 |
| - TreeNode root = new TreeNode(postorder[postorderEnd]); |
45 |
| - int inRoot = inorderMap.get(postorder[postorderEnd]); |
46 |
| - int numsLeft = inRoot - inorderStart; |
| 41 | + private TreeNode buildTreeRecursively(Map<Integer, Integer> inorderMap, int inorderStart, |
| 42 | + int inorderEnd, int[] postorder, int postorderStart, int postorderEnd) { |
| 43 | + if (postorderStart > postorderEnd || inorderStart > inorderEnd) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + TreeNode root = new TreeNode(postorder[postorderEnd]); |
| 47 | + int inRoot = inorderMap.get(postorder[postorderEnd]); |
| 48 | + int numsLeft = inRoot - inorderStart; |
47 | 49 |
|
48 |
| - /**It's easy to understand and remember: |
49 |
| - * for the indices of inorder array: |
50 |
| - * inStart and inRoot-1 as new start and end indices |
51 |
| - * inRoot+1 and inEnd as new start and end indices |
52 |
| - * |
53 |
| - * this is easy to understand and remember: since inRoot is already been used in this recursion call, so we're going to use inRoot-1 and inRoot+1 for next recursion call |
54 |
| - * |
55 |
| - * for the indices of postorder array: |
56 |
| - * postorderStart and postorderStart+numsLeft-1 should be the new start and end indices |
57 |
| - * postorderStart+numsLeft and postorderEnd-1 should be the new start and end indices |
58 |
| - * |
59 |
| - * this is also easy to understand and remember: |
60 |
| - * since the last one in postorder is the root and we have used it in this recursion call already, so the end is definitely postorderEnd-1; |
61 |
| - * then the postorderEnd for root.left is contiguous to the postorderStart of root.right, :)*/ |
62 |
| - root.left = buildTreeRecursively(inorderMap, inorderStart, inRoot - 1, postorder, postorderStart, postorderStart + numsLeft - 1); |
63 |
| - root.right = buildTreeRecursively(inorderMap, inRoot + 1, inorderEnd, postorder, postorderStart + numsLeft, postorderEnd - 1); |
64 |
| - return root; |
| 50 | + /**It's easy to understand and remember: |
| 51 | + * for the indices of inorder array: |
| 52 | + * inStart and inRoot-1 as new start and end indices |
| 53 | + * inRoot+1 and inEnd as new start and end indices |
| 54 | + * |
| 55 | + * this is easy to understand and remember: since inRoot is already been used in this recursion call, so we're going to use inRoot-1 and inRoot+1 for next recursion call |
| 56 | + * |
| 57 | + * for the indices of postorder array: |
| 58 | + * postorderStart and postorderStart+numsLeft-1 should be the new start and end indices |
| 59 | + * postorderStart+numsLeft and postorderEnd-1 should be the new start and end indices |
| 60 | + * |
| 61 | + * this is also easy to understand and remember: |
| 62 | + * since the last one in postorder is the root and we have used it in this recursion call already, so the end is definitely postorderEnd-1; |
| 63 | + * then the postorderEnd for root.left is contiguous to the postorderStart of root.right, :)*/ |
| 64 | + root.left = buildTreeRecursively(inorderMap, inorderStart, inRoot - 1, postorder, postorderStart, postorderStart + numsLeft - 1); |
| 65 | + root.right = buildTreeRecursively(inorderMap, inRoot + 1, inorderEnd, postorder, postorderStart + numsLeft, postorderEnd - 1); |
| 66 | + return root; |
| 67 | + } |
65 | 68 | }
|
66 |
| - |
67 | 69 | }
|
0 commit comments