|
|
@ -8,7 +8,7 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
给定一个二叉树的前序遍历 `preorder` 和中序遍历 `inorder` ,请从中构建二叉树,返回二叉树的根节点。
|
|
|
|
给定一个二叉树的前序遍历 `preorder` 和中序遍历 `inorder` ,请从中构建二叉树,返回二叉树的根节点。
|
|
|
|
|
|
|
|
|
|
|
|
![构建二叉树的示例数据](build_binary_tree.assets/build_tree_example.png)
|
|
|
|
![构建二叉树的示例数据](build_binary_tree_problem.assets/build_tree_example.png)
|
|
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 构建二叉树的示例数据 </p>
|
|
|
|
<p align="center"> Fig. 构建二叉树的示例数据 </p>
|
|
|
|
|
|
|
|
|
|
|
@ -31,7 +31,7 @@ comments: true
|
|
|
|
2. 查找根节点在 `inorder` 中的索引,基于该索引可将 `inorder` 划分为 `[ 9 | 3 | 1 2 7 ]` ;
|
|
|
|
2. 查找根节点在 `inorder` 中的索引,基于该索引可将 `inorder` 划分为 `[ 9 | 3 | 1 2 7 ]` ;
|
|
|
|
3. 根据 `inorder` 划分结果,可得左子树和右子树分别有 1 个和 3 个节点,从而可将 `preorder` 划分为 `[ 3 | 9 | 2 1 7 ]` ;
|
|
|
|
3. 根据 `inorder` 划分结果,可得左子树和右子树分别有 1 个和 3 个节点,从而可将 `preorder` 划分为 `[ 3 | 9 | 2 1 7 ]` ;
|
|
|
|
|
|
|
|
|
|
|
|
![在前序和中序遍历中划分子树](build_binary_tree.assets/build_tree_preorder_inorder_division.png)
|
|
|
|
![在前序和中序遍历中划分子树](build_binary_tree_problem.assets/build_tree_preorder_inorder_division.png)
|
|
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 在前序和中序遍历中划分子树 </p>
|
|
|
|
<p align="center"> Fig. 在前序和中序遍历中划分子树 </p>
|
|
|
|
|
|
|
|
|
|
|
@ -55,7 +55,7 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
请注意,右子树根节点索引中的 $(m-l)$ 的含义是“左子树的节点数量”,建议配合下图理解。
|
|
|
|
请注意,右子树根节点索引中的 $(m-l)$ 的含义是“左子树的节点数量”,建议配合下图理解。
|
|
|
|
|
|
|
|
|
|
|
|
![根节点和左右子树的索引区间表示](build_binary_tree.assets/build_tree_division_pointers.png)
|
|
|
|
![根节点和左右子树的索引区间表示](build_binary_tree_problem.assets/build_tree_division_pointers.png)
|
|
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 根节点和左右子树的索引区间表示 </p>
|
|
|
|
<p align="center"> Fig. 根节点和左右子树的索引区间表示 </p>
|
|
|
|
|
|
|
|
|
|
|
@ -88,7 +88,7 @@ comments: true
|
|
|
|
l: int,
|
|
|
|
l: int,
|
|
|
|
r: int,
|
|
|
|
r: int,
|
|
|
|
) -> TreeNode | None:
|
|
|
|
) -> TreeNode | None:
|
|
|
|
"""构建二叉树 DFS"""
|
|
|
|
"""构建二叉树:分治"""
|
|
|
|
# 子树区间为空时终止
|
|
|
|
# 子树区间为空时终止
|
|
|
|
if r - l < 0:
|
|
|
|
if r - l < 0:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
@ -96,9 +96,9 @@ comments: true
|
|
|
|
root = TreeNode(preorder[i])
|
|
|
|
root = TreeNode(preorder[i])
|
|
|
|
# 查询 m ,从而划分左右子树
|
|
|
|
# 查询 m ,从而划分左右子树
|
|
|
|
m = hmap[preorder[i]]
|
|
|
|
m = hmap[preorder[i]]
|
|
|
|
# 递归构建左子树
|
|
|
|
# 子问题:构建左子树
|
|
|
|
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1)
|
|
|
|
root.left = dfs(preorder, inorder, hmap, i + 1, l, m - 1)
|
|
|
|
# 递归构建右子树
|
|
|
|
# 子问题:构建右子树
|
|
|
|
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r)
|
|
|
|
root.right = dfs(preorder, inorder, hmap, i + 1 + m - l, m + 1, r)
|
|
|
|
# 返回根节点
|
|
|
|
# 返回根节点
|
|
|
|
return root
|
|
|
|
return root
|
|
|
@ -178,34 +178,34 @@ comments: true
|
|
|
|
下图展示了构建二叉树的递归过程,各个节点是在向下“递”的过程中建立的,而各条边是在向上“归”的过程中建立的。
|
|
|
|
下图展示了构建二叉树的递归过程,各个节点是在向下“递”的过程中建立的,而各条边是在向上“归”的过程中建立的。
|
|
|
|
|
|
|
|
|
|
|
|
=== "<1>"
|
|
|
|
=== "<1>"
|
|
|
|
![built_tree_step1](build_binary_tree.assets/built_tree_step1.png)
|
|
|
|
![构建二叉树的递归过程](build_binary_tree_problem.assets/built_tree_step1.png)
|
|
|
|
|
|
|
|
|
|
|
|
=== "<2>"
|
|
|
|
=== "<2>"
|
|
|
|
![built_tree_step2](build_binary_tree.assets/built_tree_step2.png)
|
|
|
|
![built_tree_step2](build_binary_tree_problem.assets/built_tree_step2.png)
|
|
|
|
|
|
|
|
|
|
|
|
=== "<3>"
|
|
|
|
=== "<3>"
|
|
|
|
![built_tree_step3](build_binary_tree.assets/built_tree_step3.png)
|
|
|
|
![built_tree_step3](build_binary_tree_problem.assets/built_tree_step3.png)
|
|
|
|
|
|
|
|
|
|
|
|
=== "<4>"
|
|
|
|
=== "<4>"
|
|
|
|
![built_tree_step4](build_binary_tree.assets/built_tree_step4.png)
|
|
|
|
![built_tree_step4](build_binary_tree_problem.assets/built_tree_step4.png)
|
|
|
|
|
|
|
|
|
|
|
|
=== "<5>"
|
|
|
|
=== "<5>"
|
|
|
|
![built_tree_step5](build_binary_tree.assets/built_tree_step5.png)
|
|
|
|
![built_tree_step5](build_binary_tree_problem.assets/built_tree_step5.png)
|
|
|
|
|
|
|
|
|
|
|
|
=== "<6>"
|
|
|
|
=== "<6>"
|
|
|
|
![built_tree_step6](build_binary_tree.assets/built_tree_step6.png)
|
|
|
|
![built_tree_step6](build_binary_tree_problem.assets/built_tree_step6.png)
|
|
|
|
|
|
|
|
|
|
|
|
=== "<7>"
|
|
|
|
=== "<7>"
|
|
|
|
![built_tree_step7](build_binary_tree.assets/built_tree_step7.png)
|
|
|
|
![built_tree_step7](build_binary_tree_problem.assets/built_tree_step7.png)
|
|
|
|
|
|
|
|
|
|
|
|
=== "<8>"
|
|
|
|
=== "<8>"
|
|
|
|
![built_tree_step8](build_binary_tree.assets/built_tree_step8.png)
|
|
|
|
![built_tree_step8](build_binary_tree_problem.assets/built_tree_step8.png)
|
|
|
|
|
|
|
|
|
|
|
|
=== "<9>"
|
|
|
|
=== "<9>"
|
|
|
|
![built_tree_step9](build_binary_tree.assets/built_tree_step9.png)
|
|
|
|
![built_tree_step9](build_binary_tree_problem.assets/built_tree_step9.png)
|
|
|
|
|
|
|
|
|
|
|
|
=== "<10>"
|
|
|
|
=== "<10>"
|
|
|
|
![built_tree_step10](build_binary_tree.assets/built_tree_step10.png)
|
|
|
|
![built_tree_step10](build_binary_tree_problem.assets/built_tree_step10.png)
|
|
|
|
|
|
|
|
|
|
|
|
设树的节点数量为 $n$ ,初始化每一个节点(执行一个递归函数 `dfs()` )使用 $O(1)$ 时间。**因此总体时间复杂度为 $O(n)$** 。
|
|
|
|
设树的节点数量为 $n$ ,初始化每一个节点(执行一个递归函数 `dfs()` )使用 $O(1)$ 时间。**因此总体时间复杂度为 $O(n)$** 。
|
|
|
|
|
|
|
|
|