1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { return findAncestor(root, p, q); } public static TreeNode findAncestor(TreeNode root, TreeNode p, TreeNode q){ if(p.val > root.val && q.val< root.val || p.val < root.val && q.val> root.val || p.val == root.val || q.val == root.val){ return root; } if(root.val > p.val){ return findAncestor(root.left, p , q); }else{ return findAncestor(root.right, p , q); } } }
|