mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	Update binary_search_tree
This commit is contained in:
		@ -28,7 +28,7 @@ func (bst *binarySearchTree) getRoot() *TreeNode {
 | 
				
			|||||||
	return bst.root
 | 
						return bst.root
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* 获取中序遍历的下一个结点 */
 | 
					/* 获取中序遍历的下一个结点(仅适用于 root 有左子结点的情况) */
 | 
				
			||||||
func (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode {
 | 
					func (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode {
 | 
				
			||||||
	if node == nil {
 | 
						if node == nil {
 | 
				
			||||||
		return node
 | 
							return node
 | 
				
			||||||
 | 
				
			|||||||
@ -581,6 +581,16 @@ comments: true
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        return cur;
 | 
					        return cur;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
 | 
				
			||||||
 | 
					    public TreeNode getInOrderNext(TreeNode root) {
 | 
				
			||||||
 | 
					        if (root == null) return root;
 | 
				
			||||||
 | 
					        // 循环访问左子结点,直到叶结点时为最小结点,跳出
 | 
				
			||||||
 | 
					        while (root.left != null) {
 | 
				
			||||||
 | 
					            root = root.left;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return root;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "C++"
 | 
					=== "C++"
 | 
				
			||||||
@ -623,6 +633,16 @@ comments: true
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        return cur;
 | 
					        return cur;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
 | 
				
			||||||
 | 
					    TreeNode* getInOrderNext(TreeNode* root) {
 | 
				
			||||||
 | 
					        if (root == nullptr) return root;
 | 
				
			||||||
 | 
					        // 循环访问左子结点,直到叶结点时为最小结点,跳出
 | 
				
			||||||
 | 
					        while (root->left != nullptr) {
 | 
				
			||||||
 | 
					            root = root->left;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return root;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "Python"
 | 
					=== "Python"
 | 
				
			||||||
@ -672,6 +692,15 @@ comments: true
 | 
				
			|||||||
            # 将 nex 的值复制给 cur
 | 
					            # 将 nex 的值复制给 cur
 | 
				
			||||||
            cur.val = tmp
 | 
					            cur.val = tmp
 | 
				
			||||||
        return cur
 | 
					        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"
 | 
					=== "Go"
 | 
				
			||||||
@ -731,6 +760,18 @@ comments: true
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        return cur
 | 
					        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"
 | 
					=== "JavaScript"
 | 
				
			||||||
@ -773,6 +814,16 @@ comments: true
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        return cur;
 | 
					        return cur;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
 | 
				
			||||||
 | 
					    function getInOrderNext(root) {
 | 
				
			||||||
 | 
					        if (root === null) return root;
 | 
				
			||||||
 | 
					        // 循环访问左子结点,直到叶结点时为最小结点,跳出
 | 
				
			||||||
 | 
					        while (root.left !== null) {
 | 
				
			||||||
 | 
					            root = root.left;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return root;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "TypeScript"
 | 
					=== "TypeScript"
 | 
				
			||||||
@ -826,6 +877,18 @@ comments: true
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        return cur;
 | 
					        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"
 | 
					=== "C"
 | 
				
			||||||
@ -887,6 +950,18 @@ comments: true
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        return cur;
 | 
					        return cur;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
 | 
				
			||||||
 | 
					    private TreeNode? getInOrderNext(TreeNode? root)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        if (root == null) return root;
 | 
				
			||||||
 | 
					        // 循环访问左子结点,直到叶结点时为最小结点,跳出
 | 
				
			||||||
 | 
					        while (root.left != null)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            root = root.left;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return root;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    ```
 | 
					    ```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
=== "Swift"
 | 
					=== "Swift"
 | 
				
			||||||
@ -944,6 +1019,19 @@ comments: true
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        return cur
 | 
					        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"
 | 
					=== "Zig"
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user