Lowest Common Ancestor of a Binary Search Tree
二叉搜索书上两个节点. 公共节点肯定是大小在两个节点之间的.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root.val < Math.min(p.val,q.val))
return lowestCommonAncestor(root.right,p,q);
if(root.val > Math.max(p.val,q.val))
return lowestCommonAncestor(root.left,p,q);
return root;
}
}