8ms Java Solution, beats 100%


  • 0
    	public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
    		int max = Math.max(p.val, q.val);
    		int min = Math.min(p.val, q.val);
    		while (!(max >= root.val && min <= root.val))
    			root = root.val < min ? root.right : root.left;
    		return root;
    	}
    

Log in to reply
 

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