This 4 line solution is pretty but its not efficient. You always walk the entire tree.

Imagine if you're tree is enormous, but the first 2 elements are your match.

Also consider that you find both children on the left side of a really large tree, you still walk the entire right side before popping out.

My solution (which I'm not claiming to be the prettiest or most efficient)

```
// assumes a and b and ancestor are not null
// Time Complexity: O(n) Worst case we may have to touch every node
// Space Complexity: O(n) Worst case if very unbalanced tree. Typical would be O(Log(n)).
private static int findLCA(TreeNode root,
Integer a, Integer b, Ref<TreeNode> ancestor) {
if (root == null)
return 0;
int m = 0;
if (a.equals(root.val))
m++;
if (b.equals(root.val))
m++;
if (m != 2)
m += findLCA(root.left, a, b, ancestor);
if (m != 2)
m += findLCA(root.right, a, b, ancestor);
if (m == 2)
if (ancestor.val == null)
ancestor.val = root;
return m;
}
```