Skip to content
This repository has been archived by the owner on Aug 14, 2024. It is now read-only.

Latest commit

 

History

History
26 lines (20 loc) · 621 Bytes

285.md

File metadata and controls

26 lines (20 loc) · 621 Bytes

285. Inorder Successor in BST

Description

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.

Code

class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
    	if (root == null){
            return null;
        }
        if (root.val <= p.val){
            return inorderSuccessor(root.right, p);
        }else{
            TreeNode left = inorderSuccessor(root.left, p);
            return left == null ? root : left;
        }
    }
}