mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-03 05:27:55 +08:00
Add the code to the docs.
This commit is contained in:
@ -182,7 +182,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree.py"
|
||||
# 初始化二叉树
|
||||
""" 初始化二叉树 """
|
||||
# 初始化节点
|
||||
n1 = TreeNode(val=1)
|
||||
n2 = TreeNode(val=2)
|
||||
@ -302,7 +302,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree.py"
|
||||
# 插入与删除结点
|
||||
""" 插入与删除结点 """
|
||||
p = TreeNode(0)
|
||||
# 在 n1 -> n2 中间插入结点 P
|
||||
n1.left = p
|
||||
@ -461,7 +461,7 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title=""
|
||||
“”“ 二叉树的数组表示 ”“”
|
||||
""" 二叉树的数组表示 """
|
||||
# 直接使用 None 来表示空位
|
||||
tree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
```
|
||||
|
||||
@ -51,12 +51,12 @@ comments: true
|
||||
vector<int> vec;
|
||||
while (!queue.empty()) {
|
||||
TreeNode* node = queue.front();
|
||||
queue.pop(); // 队列出队
|
||||
vec.push_back(node->val); // 保存结点
|
||||
queue.pop(); // 队列出队
|
||||
vec.push_back(node->val); // 保存结点
|
||||
if (node->left != nullptr)
|
||||
queue.push(node->left); // 左子结点入队
|
||||
queue.push(node->left); // 左子结点入队
|
||||
if (node->right != nullptr)
|
||||
queue.push(node->right); // 右子结点入队
|
||||
queue.push(node->right); // 右子结点入队
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
@ -65,7 +65,21 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_bfs.py"
|
||||
|
||||
""" 层序遍历 """
|
||||
def hier_order(root: TreeNode):
|
||||
# 初始化队列,加入根结点
|
||||
queue = collections.deque()
|
||||
queue.append(root)
|
||||
# 初始化一个列表,用于保存遍历序列
|
||||
res = []
|
||||
while queue:
|
||||
node = queue.popleft() # 队列出队
|
||||
res.append(node.val) # 保存节点值
|
||||
if node.left is not None:
|
||||
queue.append(node.left) # 左子结点入队
|
||||
if node.right is not None:
|
||||
queue.append(node.right) # 右子结点入队
|
||||
return res
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -256,7 +270,32 @@ comments: true
|
||||
=== "Python"
|
||||
|
||||
```python title="binary_tree_dfs.py"
|
||||
|
||||
""" 前序遍历 """
|
||||
def pre_order(root: typing.Optional[TreeNode]):
|
||||
if root is None:
|
||||
return
|
||||
# 访问优先级:根结点 -> 左子树 -> 右子树
|
||||
res.append(root.val)
|
||||
pre_order(root=root.left)
|
||||
pre_order(root=root.right)
|
||||
|
||||
""" 中序遍历 """
|
||||
def in_order(root: typing.Optional[TreeNode]):
|
||||
if root is None:
|
||||
return
|
||||
# 访问优先级:左子树 -> 根结点 -> 右子树
|
||||
in_order(root=root.left)
|
||||
res.append(root.val)
|
||||
in_order(root=root.right)
|
||||
|
||||
""" 后序遍历 """
|
||||
def post_order(root: typing.Optional[TreeNode]):
|
||||
if root is None:
|
||||
return
|
||||
# 访问优先级:左子树 -> 右子树 -> 根结点
|
||||
post_order(root=root.left)
|
||||
post_order(root=root.right)
|
||||
res.append(root.val)
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
@ -402,7 +441,6 @@ comments: true
|
||||
postOrder(root.right);
|
||||
list.Add(root.val);
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
!!! note
|
||||
|
||||
Reference in New Issue
Block a user