From 2da45041b1e1d927c8b1c42a291850137edadc80 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Thu, 2 Feb 2023 13:54:31 +0800 Subject: [PATCH] Update binary_search_tree --- codes/go/chapter_tree/binary_search_tree.go | 2 +- docs/chapter_tree/binary_search_tree.md | 88 +++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/codes/go/chapter_tree/binary_search_tree.go b/codes/go/chapter_tree/binary_search_tree.go index e4fd08103..85298f3ca 100644 --- a/codes/go/chapter_tree/binary_search_tree.go +++ b/codes/go/chapter_tree/binary_search_tree.go @@ -28,7 +28,7 @@ func (bst *binarySearchTree) getRoot() *TreeNode { return bst.root } -/* 获取中序遍历的下一个结点 */ +/* 获取中序遍历的下一个结点(仅适用于 root 有左子结点的情况) */ func (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode { if node == nil { return node diff --git a/docs/chapter_tree/binary_search_tree.md b/docs/chapter_tree/binary_search_tree.md index 222c8c2dc..24e3e396f 100644 --- a/docs/chapter_tree/binary_search_tree.md +++ b/docs/chapter_tree/binary_search_tree.md @@ -581,6 +581,16 @@ comments: true } return cur; } + + /* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */ + public TreeNode getInOrderNext(TreeNode root) { + if (root == null) return root; + // 循环访问左子结点,直到叶结点时为最小结点,跳出 + while (root.left != null) { + root = root.left; + } + return root; + } ``` === "C++" @@ -623,6 +633,16 @@ comments: true } return cur; } + + /* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */ + TreeNode* getInOrderNext(TreeNode* root) { + if (root == nullptr) return root; + // 循环访问左子结点,直到叶结点时为最小结点,跳出 + while (root->left != nullptr) { + root = root->left; + } + return root; + } ``` === "Python" @@ -672,6 +692,15 @@ comments: true # 将 nex 的值复制给 cur cur.val = tmp return cur + + """ 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) """ + def get_inorder_next(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + if root is None: + return root + # 循环访问左子结点,直到叶结点时为最小结点,跳出 + while root.left is not None: + root = root.left + return root ``` === "Go" @@ -731,6 +760,18 @@ comments: true } return cur } + + /* 获取中序遍历的下一个结点(仅适用于 root 有左子结点的情况) */ + func (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode { + if node == nil { + return node + } + // 循环访问左子结点,直到叶结点时为最小结点,跳出 + for node.Left != nil { + node = node.Left + } + return node + } ``` === "JavaScript" @@ -773,6 +814,16 @@ comments: true } return cur; } + + /* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */ + function getInOrderNext(root) { + if (root === null) return root; + // 循环访问左子结点,直到叶结点时为最小结点,跳出 + while (root.left !== null) { + root = root.left; + } + return root; + } ``` === "TypeScript" @@ -826,6 +877,18 @@ comments: true } return cur; } + + /* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */ + function getInOrderNext(root: TreeNode | null): TreeNode | null { + if (root === null) { + return null; + } + // 循环访问左子结点,直到叶结点时为最小结点,跳出 + while (root.left !== null) { + root = root.left; + } + return root; + } ``` === "C" @@ -887,6 +950,18 @@ comments: true } return cur; } + + /* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */ + private TreeNode? getInOrderNext(TreeNode? root) + { + if (root == null) return root; + // 循环访问左子结点,直到叶结点时为最小结点,跳出 + while (root.left != null) + { + root = root.left; + } + return root; + } ``` === "Swift" @@ -944,6 +1019,19 @@ comments: true } return cur } + + /* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */ + func getInOrderNext(root: TreeNode?) -> TreeNode? { + var root = root + if root == nil { + return root + } + // 循环访问左子结点,直到叶结点时为最小结点,跳出 + while root?.left != nil { + root = root?.left + } + return root + } ``` === "Zig"