Lowest Common Ancestor of a Binary Search Tree LeetCode solution in Java
1/**2* Definition for a binary tree node. public class TreeNode { int val; TreeNode left; TreeNode3* right; TreeNode(int x) { val = x; } }4*/5class Solution {6public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {7if (root == null || root == p || root == q) return root;89TreeNode left = lowestCommonAncestor(root.left, p, q);10TreeNode right = lowestCommonAncestor(root.right, p, q);1112if (left == null) return right;13if (right == null) return left;14else {15return root;16}17}18}