Solution with Binary Search


  • 0
    L
    function lowestCommonAncestor(root, p, q) {
        var n = root;
        if (n.val > Math.max(p.val, q.val)) {
            return lowestCommonAncestor(n.left, p, q);
        }
        if (n.val < Math.min(p.val, q.val)) {
            return lowestCommonAncestor(n.right, p, q);
        }
        return n;
    }

Log in to reply
 

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