Inorder Successor in BST

mail@pastecode.io avatar
unknown
java
2 years ago
463 B
0
Indexable
Never
public class Solution {
    TreeNode successor = null;

    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        helper(root, p);
        return successor;
    }

    void helper(TreeNode root, TreeNode p) {
        if (root == null || p == null) {
            return;
        }

        if (root.val <= p.val) {
            helper(root.right, p);
        } else {
            successor = root;
            helper(root.left, p);
        }
    }
}