You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0100-0199/0105.Construct Binary Tree from Preorder and Inorder Traversal/README_EN.md
+12-9
Original file line number
Diff line number
Diff line change
@@ -36,19 +36,22 @@
36
36
37
37
## Solutions
38
38
39
-
### Solution 1: Recursion
39
+
### Solution 1: Hash Table + Recursion
40
40
41
-
The first node $preorder[0]$ in the preorder sequence is the root node. We find the position $i$ of the root node in the inorder sequence, which divides the inorder sequence into the left subtree $inorder[0..i]$ and the right subtree $inorder[i+1..]$.
41
+
The first node $preorder[0]$ in the pre-order sequence is the root node. We find the position $k$ of the root node in the in-order sequence, which can divide the in-order sequence into the left subtree $inorder[0..k]$ and the right subtree $inorder[k+1..]$.
42
42
43
-
Through the intervals of the left and right subtrees, we can calculate the number of nodes in the left and right subtrees, assumed to be $m$ and $n$ respectively. Then in the preorder nodes, the $m$ nodes following the root node are the left subtree, and the $n$ nodes after that are the right subtree.
43
+
Through the intervals of the left and right subtrees, we can calculate the number of nodes in the left and right subtrees, assumed to be $a$ and $b$. Then in the pre-order nodes, the $a$ nodes after the root node are the left subtree, and the $b$ nodes after that are the right subtree.
44
44
45
-
We can solve this recursively.
45
+
Therefore, we design a function $dfs(i, j, n)$, where $i$ and $j$ represent the starting positions of the pre-order sequence and the in-order sequence, respectively, and $n$ represents the number of nodes. The return value of the function is the binary tree constructed with $preorder[i..i+n-1]$ as the pre-order sequence and $inorder[j..j+n-1]$ as the in-order sequence.
46
46
47
-
> Preorder traversal: traverse the root node first, then traverse the left and right subtrees; Inorder traversal: traverse the left subtree first, then traverse the root node, and finally traverse the right subtree.
47
+
The execution process of the function $dfs(i, j, n)$ is as follows:
48
48
49
-
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
49
+
- If $n \leq 0$, it means there are no nodes, return a null node.
50
+
- Take out the first node $v = preorder[i]$ of the pre-order sequence as the root node, and then use the hash table $d$ to find the position $k$ of the root node in the in-order sequence. Then the number of nodes in the left subtree is $k - j$, and the number of nodes in the right subtree is $n - k + j - 1$.
51
+
- Recursively construct the left subtree $l = dfs(i + 1, j, k - j)$ and the right subtree $r = dfs(i + 1 + k - j, k + 1, n - k + j - 1)$.
52
+
- Finally, return the binary tree with $v$ as the root node and $l$ and $r$ as the left and right subtrees, respectively.
50
53
51
-
If the node values given in the problem have duplicates, then we only need to record all the positions where each node value appears, and then recursively construct the tree.
54
+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes in the binary tree.
52
55
53
56
<!-- tabs:start -->
54
57
@@ -61,7 +64,7 @@ If the node values given in the problem have duplicates, then we only need to re
@@ -304,7 +307,7 @@ var buildTree = function (preorder, inorder) {
304
307
305
308
<!-- tabs:end -->
306
309
307
-
### Solution 2
310
+
If the node values given in the problem have duplicates, then we only need to record all the positions where each node value appears, and then recursively construct the tree.
0 commit comments