Inorder Successor in Binary Search Tree(中序后继)

Given a binary search tree and a node in it, find the in-order successor of that node in the BST.

Note: If the given node has no in-order successor in the tree, return null.

Successor

分析

利用bst左右子树和root大小关系

iterative

    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        TreeNode ret = null;
        while(root != null){
            if(root.val > p.val){
                ret = root;
                root = root.left;
            }else{
                root = root.right;
            }
        }

        return ret;
    }

recursive

Predecessor

iterative

recursive

Last updated

Was this helpful?