Fine-tune code and texts.
This commit is contained in:
parent
3628b40f44
commit
5b1a219b8b
@ -115,9 +115,9 @@ int main() {
|
|||||||
int i = 1;
|
int i = 1;
|
||||||
int l = abt.left(i), r = abt.right(i), p = abt.parent(i);
|
int l = abt.left(i), r = abt.right(i), p = abt.parent(i);
|
||||||
cout << "\n当前节点的索引为 " << i << ",值为 " << abt.val(i) << "\n";
|
cout << "\n当前节点的索引为 " << i << ",值为 " << abt.val(i) << "\n";
|
||||||
cout << "其左子节点的索引为 " << l << ",值为 " << (l != INT_MAX ? to_string(abt.val(l)) : "None") << "\n";
|
cout << "其左子节点的索引为 " << l << ",值为 " << (l != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n";
|
||||||
cout << "其右子节点的索引为 " << r << ",值为 " << (r != INT_MAX ? to_string(abt.val(r)) : "None") << "\n";
|
cout << "其右子节点的索引为 " << r << ",值为 " << (r != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n";
|
||||||
cout << "其父节点的索引为 " << p << ",值为 " << (p != INT_MAX ? to_string(abt.val(p)) : "None") << "\n";
|
cout << "其父节点的索引为 " << p << ",值为 " << (p != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n";
|
||||||
|
|
||||||
// 遍历树
|
// 遍历树
|
||||||
vector<int> res = abt.levelOrder();
|
vector<int> res = abt.levelOrder();
|
||||||
|
@ -74,12 +74,11 @@ vector<int> treeToVecor(TreeNode *root) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Free the memory allocated to a tree */
|
/* 释放二叉树内存 */
|
||||||
void freeMemoryTree(TreeNode *root) {
|
void freeMemoryTree(TreeNode *root) {
|
||||||
if (root == nullptr)
|
if (root == nullptr)
|
||||||
return;
|
return;
|
||||||
freeMemoryTree(root->left);
|
freeMemoryTree(root->left);
|
||||||
freeMemoryTree(root->right);
|
freeMemoryTree(root->right);
|
||||||
// 释放内存
|
|
||||||
delete root;
|
delete root;
|
||||||
}
|
}
|
||||||
|
@ -92,9 +92,9 @@ if __name__ == "__main__":
|
|||||||
arr = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
arr = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||||
root = list_to_tree(arr)
|
root = list_to_tree(arr)
|
||||||
print("\n初始化二叉树\n")
|
print("\n初始化二叉树\n")
|
||||||
print(f"二叉树的数组表示:")
|
print("二叉树的数组表示:")
|
||||||
print(arr)
|
print(arr)
|
||||||
print(f"二叉树的链表表示:")
|
print("二叉树的链表表示:")
|
||||||
print_tree(root)
|
print_tree(root)
|
||||||
|
|
||||||
# 数组表示下的二叉树类
|
# 数组表示下的二叉树类
|
||||||
|
@ -55,10 +55,7 @@
|
|||||||
[file]{merge_sort}-[class]{}-[func]{merge_sort}
|
[file]{merge_sort}-[class]{}-[func]{merge_sort}
|
||||||
```
|
```
|
||||||
|
|
||||||
实现合并函数 `merge()` 存在以下难点。
|
值得注意的是,`nums` 的待合并区间为 `[left, right]` ,而 `tmp` 的对应区间为 `[0, right - left]` 。
|
||||||
|
|
||||||
- **需要特别注意各个变量的含义**。`nums` 的待合并区间为 `[left, right]` ,但由于 `tmp` 仅复制了 `nums` 该区间的元素,因此 `tmp` 对应区间为 `[0, right - left]` 。
|
|
||||||
- 在比较 `tmp[i]` 和 `tmp[j]` 的大小时,**还需考虑子数组遍历完成后的索引越界问题**,即 `i > leftEnd` 和 `j > rightEnd` 的情况。索引越界的优先级是最高的,如果左子数组已经被合并完了,那么不需要继续比较,直接合并右子数组元素即可。
|
|
||||||
|
|
||||||
## 算法特性
|
## 算法特性
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user