mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-03 05:27:55 +08:00
Fine-tune code and texts.
This commit is contained in:
@ -115,9 +115,9 @@ int main() {
|
||||
int i = 1;
|
||||
int l = abt.left(i), r = abt.right(i), p = abt.parent(i);
|
||||
cout << "\n当前节点的索引为 " << i << ",值为 " << abt.val(i) << "\n";
|
||||
cout << "其左子节点的索引为 " << l << ",值为 " << (l != INT_MAX ? to_string(abt.val(l)) : "None") << "\n";
|
||||
cout << "其右子节点的索引为 " << r << ",值为 " << (r != INT_MAX ? to_string(abt.val(r)) : "None") << "\n";
|
||||
cout << "其父节点的索引为 " << p << ",值为 " << (p != INT_MAX ? to_string(abt.val(p)) : "None") << "\n";
|
||||
cout << "其左子节点的索引为 " << l << ",值为 " << (l != INT_MAX ? to_string(abt.val(l)) : "nullptr") << "\n";
|
||||
cout << "其右子节点的索引为 " << r << ",值为 " << (r != INT_MAX ? to_string(abt.val(r)) : "nullptr") << "\n";
|
||||
cout << "其父节点的索引为 " << p << ",值为 " << (p != INT_MAX ? to_string(abt.val(p)) : "nullptr") << "\n";
|
||||
|
||||
// 遍历树
|
||||
vector<int> res = abt.levelOrder();
|
||||
|
||||
@ -74,12 +74,11 @@ vector<int> treeToVecor(TreeNode *root) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Free the memory allocated to a tree */
|
||||
/* 释放二叉树内存 */
|
||||
void freeMemoryTree(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
freeMemoryTree(root->left);
|
||||
freeMemoryTree(root->right);
|
||||
// 释放内存
|
||||
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]
|
||||
root = list_to_tree(arr)
|
||||
print("\n初始化二叉树\n")
|
||||
print(f"二叉树的数组表示:")
|
||||
print("二叉树的数组表示:")
|
||||
print(arr)
|
||||
print(f"二叉树的链表表示:")
|
||||
print("二叉树的链表表示:")
|
||||
print_tree(root)
|
||||
|
||||
# 数组表示下的二叉树类
|
||||
|
||||
Reference in New Issue
Block a user