chapter_tree/binary_tree_traversal/ #25
Replies: 79 comments 61 replies
-
点进去好像没有看见go的代码显示? 想知道有没有机会贡献go的代码 |
Beta Was this translation helpful? Give feedback.
-
结点总数为 n 的二叉树的高度 Log2(n+1) - 1 |
Beta Was this translation helpful? Give feedback.
-
所谓的前中后序遍历:先遍历父节点为preOrder,第二个遍历父节点为inOrder,最后遍历父节点为postOrder |
Beta Was this translation helpful? Give feedback.
-
遍历方式:
|
Beta Was this translation helpful? Give feedback.
-
用栈实现 dfs |
Beta Was this translation helpful? Give feedback.
-
这里的前中后序,指的是根结点吧 |
Beta Was this translation helpful? Give feedback.
-
问:为什么DFS遍历二叉树有前、中、后三种顺序,分别有什么用呢? |
Beta Was this translation helpful? Give feedback.
-
递归利用栈的数据结构,代码的真少啊 |
Beta Was this translation helpful? Give feedback.
-
为啥没有c的代码,描述数据结构最好的代码就是c啊 |
Beta Was this translation helpful? Give feedback.
-
你这个前、中、后序遍历的图简直太好了,把我至今抽象的理解具现化了 ❤️ |
Beta Was this translation helpful? Give feedback.
-
binary_tree_bfs.c
|
Beta Was this translation helpful? Give feedback.
-
空间复杂度:在最差情况下,即满二叉树时,遍历到最底层之前,队列中最多同时存在 (N+1)/2个节点——满二叉树,bfs遍历到最底层之前,队列中的节点就是叶节点2的h次方吧? |
Beta Was this translation helpful? Give feedback.
-
太牛啦,每次面试都需要复习一遍,这次永远不会忘记了 |
Beta Was this translation helpful? Give feedback.
-
前序遍历结合栈使用迭代
|
Beta Was this translation helpful? Give feedback.
-
太棒了,对递归了解更多了一点,其他的广度优先和深度优先遍历是不是也用递归 |
Beta Was this translation helpful? Give feedback.
-
广度优先搜索不是应该分析最后一层的节点数量吗,这里为什么要写到底最底层之前? |
Beta Was this translation helpful? Give feedback.
-
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
Beta Was this translation helpful? Give feedback.
-
python3迭代实现已通过力扣 def pre_order(root: TreeNode | None):
"""前序遍历"""
# 访问优先级:根节点 -> 左子树 -> 右子树
stack = [root]
while stack:
node = stack.pop()
if node is None:
continue
res.append(node.val)
stack.append(node.right)
stack.append(node.left)
def in_order(root: TreeNode | None):
"""中序遍历"""
# 访问优先级:左子树 -> 根节点 -> 右子树
stack = [root]
while stack:
node = stack[-1]
if node is None:
stack.pop()
if not stack:
return
node = stack.pop()
res.append(node.val)
stack.append(node.right)
continue
stack.append(node.left)
assert False
def post_order(root: TreeNode | None):
"""后序遍历"""
# 访问优先级:左子树 -> 右子树 -> 根节点
stack = [root]
while stack:
node = stack.pop()
if node is None:
continue
res.append(node.val)
stack.append(node.left)
stack.append(node.right)
res.reverse() |
Beta Was this translation helpful? Give feedback.
-
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
Beta Was this translation helpful? Give feedback.
-
pre_order(root=root.right) -> pre_order(root.right) 会不会更好? |
Beta Was this translation helpful? Give feedback.
-
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
Beta Was this translation helpful? Give feedback.
-
day06 |
Beta Was this translation helpful? Give feedback.
-
太棒了 |
Beta Was this translation helpful? Give feedback.
-
全文背诵 |
Beta Was this translation helpful? Give feedback.
-
"深度优先遍历就像是绕着整棵二叉树的外围“走”一圈,在每个节点都会遇到三个位置,分别对应前序遍历、中序遍历和后序遍历。" |
Beta Was this translation helpful? Give feedback.
-
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
总结:1.层序遍历:用队列 添加一层节点 处理一层节点 然后迭代;2.前中后序遍历:根据根的遍历顺序来划分,根在前(根、左、右)处理为前序,中间(左、根、右)处理跟为中序,最后(左、右、根)处理跟为后续。 |
Beta Was this translation helpful? Give feedback.
-
这是来自QQ邮箱的假期自动回复邮件。
您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。
|
Beta Was this translation helpful? Give feedback.
-
太绕了,但是对递归和二叉树的理解更深一点了,对于接触指针比较少的python选手理解起来有点费劲 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
chapter_tree/binary_tree_traversal/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_tree/binary_tree_traversal/
Beta Was this translation helpful? Give feedback.
All reactions