mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-03 18:50:58 +08:00
build
This commit is contained in:
@@ -429,11 +429,14 @@ status: new
|
||||
=== "<9>"
|
||||

|
||||
|
||||
=== "<10>"
|
||||

|
||||
|
||||
<p align="center"> 图 12-8 构建二叉树的递归过程 </p>
|
||||
|
||||
每个递归函数内的前序遍历 `preorder` 和中序遍历 `inorder` 的划分结果如图 12-9 所示。
|
||||
|
||||

|
||||
|
||||
<p align="center"> 图 12-9 built_tree_overall </p>
|
||||
|
||||
设树的节点数量为 $n$ ,初始化每一个节点(执行一个递归函数 `dfs()` )使用 $O(1)$ 时间。**因此总体时间复杂度为 $O(n)$** 。
|
||||
|
||||
哈希表存储 `inorder` 元素到索引的映射,空间复杂度为 $O(n)$ 。最差情况下,即二叉树退化为链表时,递归深度达到 $n$ ,使用 $O(n)$ 的栈帧空间。**因此总体空间复杂度为 $O(n)$** 。
|
||||
|
||||
Reference in New Issue
Block a user