Share explanation for top votes solution


  • 0
    F

    If we convert the problem to give a BST tree which doesn't contain p node, let you return the least number which is greater than p.
    the codes should like the following, and do a little modification you will get the solution for this problem.

        public TreeNode findCeiling(TreeNode root, TreeNode p) {
            if (root == null) return root;
            if (root.val < p.val) return findCeiling(root.right, p);
            TreeNode node = findCeiling(root.left, p);
            return node == null ? root : node;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.