Add the code to the docs.

pull/138/head
Yudong Jin 2 years ago
parent dbb25003ec
commit 449258f0b0

@ -15,12 +15,12 @@ vector<int> hierOrder(TreeNode* root) {
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;
}

@ -12,13 +12,14 @@ from include import *
""" Driver Code """
if __name__ == "__main__":
# 初始化二叉树
""" 初始化二叉树 """
# 初始化节点
n1 = TreeNode(val=1)
n2 = TreeNode(val=2)
n3 = TreeNode(val=3)
n4 = TreeNode(val=4)
n5 = TreeNode(val=5)
# 构建引用指向(即指针)
n1.left = n2
n1.right = n3
n2.left = n4
@ -26,15 +27,13 @@ if __name__ == "__main__":
print("\n初始化二叉树\n")
print_tree(n1)
# 插入与删除结点
""" 插入与删除结点 """
P = TreeNode(0)
# 在 n1 -> n2 中间插入节点 P
n1.left = P
P.left = n2
print("\n插入结点 P 后\n")
print_tree(n1)
# 删除结点
n1.left = n2
print("\n删除结点 P 后\n");

@ -14,22 +14,18 @@ from include import *
""" 层序遍历 """
def hier_order(root: TreeNode):
# 初始化队列,加入根结点
queue: typing.Deque[TreeNode] = collections.deque()
queue = collections.deque()
queue.append(root)
# 初始化一个列表,用于保存遍历序列
result = []
res = []
while queue:
# 队列出队
node = queue.popleft()
# 保存节点值
result.append(node.val)
node = queue.popleft() # 队列出队
res.append(node.val) # 保存节点值
if node.left is not None:
# 左子结点入队
queue.append(node.left)
queue.append(node.left) # 左子结点入队
if node.right is not None:
# 右子结点入队
queue.append(node.right)
return result
queue.append(node.right) # 右子结点入队
return res
""" Driver Code """
@ -41,6 +37,6 @@ if __name__ == "__main__":
print_tree(root)
# 层序遍历
result = hier_order(root)
print("\n层序遍历的结点打印序列 = ", result)
assert result == [1, 2, 3, 4, 5, 6, 7]
res = hier_order(root)
print("\n层序遍历的结点打印序列 = ", res)
assert res == [1, 2, 3, 4, 5, 6, 7]

@ -11,40 +11,34 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
result = []
res = []
""" 前序遍历二叉树 """
""" 前序遍历 """
def pre_order(root: typing.Optional[TreeNode]):
if root is None:
return
# 访问优先级:根结点 -> 左子树 -> 右子树
result.append(root.val)
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)
result.append(root.val)
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)
result.append(root.val)
res.append(root.val)
""" Driver Code """
@ -56,19 +50,19 @@ if __name__ == "__main__":
print_tree(root)
# 前序遍历
result.clear()
res.clear()
pre_order(root)
print("\n前序遍历的结点打印序列 = ", result)
assert result == [1, 2, 4, 5, 3, 6, 7]
print("\n前序遍历的结点打印序列 = ", res)
assert res == [1, 2, 4, 5, 3, 6, 7]
# 中序遍历
result.clear()
res.clear()
in_order(root)
print("\n中序遍历的结点打印序列 = ", result)
assert result == [4, 2, 5, 1, 6, 3, 7]
print("\n中序遍历的结点打印序列 = ", res)
assert res == [4, 2, 5, 1, 6, 3, 7]
# 后序遍历
result.clear()
res.clear()
post_order(root)
print("\n后序遍历的结点打印序列 = ", result)
assert result == [4, 5, 2, 6, 7, 3, 1]
print("\n后序遍历的结点打印序列 = ", res)
assert res == [4, 5, 2, 6, 7, 3, 1]

@ -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

Loading…
Cancel
Save