Java,while loop,5 lines


  • 0
    Z
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
            while (root != null) {
                if (root.val < p.val && root.val < q.val) root = root.right;
                else if (root.val > p.val && root.val > q.val) root = root.left;
                else return root;
            }
            return root;
    }
    

Log in to reply
 

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