/* * 235. Lowest Common Ancestor of a Binary Search Tree * 2016-6-15 By Mingyang * 这题目更简单,下面两种方法都适用,现在给出更直接的方法 */ public TreeNode lowestCommonAncestor3(TreeNode root, TreeNode p, TreeNode q) { if (p.val == root.val || q.val == root.val) return root; if ((p.val < root.val && q.val > root.val)|| (p.val > root.val && q.val < root.val)) return root; if (p.val < root.val) { return lowestCommonAncestor(root.left, p, q); } else { return lowestCommonAncestor(root.right, p, q); } }