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