|
14 | 14 | */
|
15 | 15 | public class _105 {
|
16 | 16 |
|
17 |
| - /** |
18 |
| - * credit: https://discuss.leetcode.com/topic/29838/5ms-java-clean-solution-with-caching |
19 |
| - * use HashMap as the cache so that accessing inorder index becomes O(1) time |
20 |
| - * Note: The first element of preorder array is the root! |
21 |
| - */ |
22 |
| - public TreeNode buildTree(int[] preorder, int[] inorder) { |
23 |
| - Map<Integer, Integer> inorderMap = new HashMap(); |
24 |
| - for (int i = 0; i < inorder.length; i++) { |
25 |
| - inorderMap.put(inorder[i], i); |
| 17 | + public static class Solution1 { |
| 18 | + /** |
| 19 | + * credit: https://discuss.leetcode.com/topic/29838/5ms-java-clean-solution-with-caching use |
| 20 | + * HashMap as the cache so that accessing inorder index becomes O(1) time Note: The first |
| 21 | + * element of preorder array is the root! |
| 22 | + */ |
| 23 | + public TreeNode buildTree(int[] preorder, int[] inorder) { |
| 24 | + Map<Integer, Integer> inorderMap = new HashMap(); |
| 25 | + for (int i = 0; i < inorder.length; i++) { |
| 26 | + inorderMap.put(inorder[i], i); |
| 27 | + } |
| 28 | + |
| 29 | + /**At the beginning, both start from 0 to nums.length-1*/ |
| 30 | + return buildTree(preorder, 0, preorder.length - 1, inorderMap, 0, inorder.length - 1); |
26 | 31 | }
|
27 | 32 |
|
28 |
| - /**At the beginning, both start from 0 to nums.length-1*/ |
29 |
| - return buildTree(preorder, 0, preorder.length - 1, inorderMap, 0, inorder.length - 1); |
30 |
| - } |
31 |
| - |
32 |
| - private TreeNode buildTree(int[] preorder, int preStart, int preEnd, Map<Integer, Integer> inorderMap, int inStart, int inEnd) { |
33 |
| - if (preStart > preEnd || inStart > inEnd) { |
34 |
| - return null; |
| 33 | + private TreeNode buildTree(int[] preorder, int preStart, int preEnd, |
| 34 | + Map<Integer, Integer> inorderMap, int inStart, int inEnd) { |
| 35 | + if (preStart > preEnd || inStart > inEnd) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + TreeNode root = new TreeNode(preorder[preStart]); |
| 40 | + int inRoot = inorderMap.get(preorder[preStart]); |
| 41 | + int numsLeft = inRoot - inStart; |
| 42 | + |
| 43 | + /**It's easy to understand and remember: |
| 44 | + * for the indices of inorder array: |
| 45 | + * root.left should be inStart and inRoot-1 as new start and end indices |
| 46 | + * root.right should be inRoot+1 and inEnd as new start and end indices |
| 47 | + * |
| 48 | + * since inRoot is being used already in this recursion call, that's why we use inRoot-1 and inRoot+1 |
| 49 | + * this part is the same for both Leetcode 105 and Leetcode 106.*/ |
| 50 | + root.left = buildTree(preorder, preStart + 1, preStart + numsLeft, inorderMap, inStart, inRoot - 1); |
| 51 | + root.right = buildTree(preorder, preStart + numsLeft + 1, preEnd, inorderMap, inRoot + 1, inEnd); |
| 52 | + return root; |
35 | 53 | }
|
36 |
| - |
37 |
| - TreeNode root = new TreeNode(preorder[preStart]); |
38 |
| - int inRoot = inorderMap.get(preorder[preStart]); |
39 |
| - int numsLeft = inRoot - inStart; |
40 |
| - |
41 |
| - /**It's easy to understand and remember: |
42 |
| - * for the indices of inorder array: |
43 |
| - * root.left should be inStart and inRoot-1 as new start and end indices |
44 |
| - * root.right should be inRoot+1 and inEnd as new start and end indices |
45 |
| - * |
46 |
| - * since inRoot is being used already in this recursion call, that's why we use inRoot-1 and inRoot+1 |
47 |
| - * this part is the same for both Leetcode 105 and Leetcode 106.*/ |
48 |
| - root.left = buildTree(preorder, preStart + 1, preStart + numsLeft, inorderMap, inStart, inRoot - 1); |
49 |
| - root.right = buildTree(preorder, preStart + numsLeft + 1, preEnd, inorderMap, inRoot + 1, inEnd); |
50 |
| - return root; |
51 | 54 | }
|
52 |
| - |
53 | 55 | }
|
0 commit comments