|
| 1 | +--- |
| 2 | +id: Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal |
| 3 | +title: Construct Binary Tree from Preorder and Inorder Traversal |
| 4 | +sidebar_label: 0105-Construct-Binary-Tree-from-Preorder-and-Inorder-Traversal |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Hash Tabel |
| 8 | + - Divide and Conquer |
| 9 | + - Tree |
| 10 | + - Binary Tree |
| 11 | +description: "Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree." |
| 12 | +--- |
| 13 | + |
| 14 | + |
| 15 | +### Problem Description |
| 16 | + |
| 17 | +Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +#### Example 1 |
| 22 | + |
| 23 | +- **Input:** `preorder = [3,9,20,15,7], inorder=[9,3,15,20,7]` |
| 24 | +- **Output:** `[3,9,30,null,null,15,7]` |
| 25 | + |
| 26 | +#### Example 2 |
| 27 | + |
| 28 | +- **Input:** `preorder = [-1], inorder=[-1]` |
| 29 | +- **Output:** `[-1]` |
| 30 | + |
| 31 | +### Constraints |
| 32 | + |
| 33 | +- $1 \leq \text{preorder.length} \leq 3000$ |
| 34 | +- $\text{inorder.length} == \text{preorder.length}$ |
| 35 | +- $-3000 \leq \text{preorder}[i], \text{inorder}[i] \leq 3000$ |
| 36 | +- preorder and inorder consist of unique values. |
| 37 | +- Each value of inorder also appears in preorder. |
| 38 | +- preorder is guaranteed to be the preorder traversal of the tree. |
| 39 | +- inorder is guaranteed to be the inorder traversal of the tree. |
| 40 | + |
| 41 | +### Solution Code |
| 42 | + |
| 43 | +#### Python |
| 44 | + |
| 45 | +```python |
| 46 | +class Solution: |
| 47 | + def buildTree(self, P: List[int], I: List[int]) -> TreeNode: |
| 48 | + M = {I[i]: i for i in range(len(I))} |
| 49 | + return self.splitTree(P, M, 0, 0, len(P)-1) |
| 50 | + |
| 51 | + def splitTree(self, P: List[int], M: dict, pix: int, ileft: int, iright: int) -> TreeNode: |
| 52 | + rval = P[pix] |
| 53 | + root, imid = TreeNode(rval), M[rval] |
| 54 | + if imid > ileft: |
| 55 | + root.left = self.splitTree(P, M, pix+1, ileft, imid-1) |
| 56 | + if imid < iright: |
| 57 | + root.right = self.splitTree(P, M, pix+imid-ileft+1, imid+1, iright) |
| 58 | + return root |
| 59 | +``` |
| 60 | + |
| 61 | +#### Java |
| 62 | + |
| 63 | +```java |
| 64 | +class Solution { |
| 65 | + public TreeNode buildTree(int[] P, int[] I) { |
| 66 | + Map<Integer, Integer> M = new HashMap<>(); |
| 67 | + for (int i = 0; i < I.length; i++) |
| 68 | + M.put(I[i], i); |
| 69 | + return splitTree(P, M, 0, 0, I.length-1); |
| 70 | + } |
| 71 | + |
| 72 | + private TreeNode splitTree(int[] P, Map<Integer, Integer> M, int pix, int ileft, int iright) { |
| 73 | + int rval = P[pix], imid = M.get(rval); |
| 74 | + TreeNode root = new TreeNode(rval); |
| 75 | + if (imid > ileft) |
| 76 | + root.left = splitTree(P, M, pix+1, ileft, imid-1); |
| 77 | + if (imid < iright) |
| 78 | + root.right = splitTree(P, M, pix+imid-ileft+1, imid+1, iright); |
| 79 | + return root; |
| 80 | + } |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +#### C++ |
| 85 | + |
| 86 | +```cpp |
| 87 | +class Solution { |
| 88 | +public: |
| 89 | + TreeNode* buildTree(vector<int>& P, vector<int>& I) { |
| 90 | + unordered_map<int, int> M; |
| 91 | + for (int i = 0; i < I.size(); i++) |
| 92 | + M[I[i]] = i; |
| 93 | + return splitTree(P, M, 0, 0, I.size()-1); |
| 94 | + } |
| 95 | + |
| 96 | +private: |
| 97 | + TreeNode* splitTree(vector<int>& P, unordered_map<int, int>& M, int pix, int ileft, int iright) { |
| 98 | + int rval = P[pix], imid = M[rval]; |
| 99 | + TreeNode* root = new TreeNode(rval); |
| 100 | + if (imid > ileft) |
| 101 | + root->left = splitTree(P, M, pix+1, ileft, imid-1); |
| 102 | + if (imid < iright) |
| 103 | + root->right = splitTree(P, M, pix+imid-ileft+1, imid+1, iright); |
| 104 | + return root; |
| 105 | + } |
| 106 | +}; |
| 107 | +``` |
| 108 | +#### Javascript |
| 109 | +
|
| 110 | +```javascript |
| 111 | +var buildTree = function(P, I) { |
| 112 | + let M = new Map() |
| 113 | + for (let i = 0; i < I.length; i++) |
| 114 | + M.set(I[i], i) |
| 115 | + return splitTree(P, M, 0, 0, I.length-1) |
| 116 | +}; |
| 117 | +
|
| 118 | +var splitTree = function(P, M, pix, ileft, iright) { |
| 119 | + let rval = P[pix], |
| 120 | + root = new TreeNode(rval), |
| 121 | + imid = M.get(rval) |
| 122 | + if (imid > ileft) |
| 123 | + root.left = splitTree(P, M, pix+1, ileft, imid-1) |
| 124 | + if (imid < iright) |
| 125 | + root.right = splitTree(P, M, pix+imid-ileft+1, imid+1, iright) |
| 126 | + return root |
| 127 | +} |
| 128 | +``` |
0 commit comments