# Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______6______
/              \
___2__          ___8__
/      \        /      \
0      _4       7       9
/  \
3   5


For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

/**
* 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) {
int largerVal = p.val > q.val? p.val : q.val;
int smallerVal = p.val == largerVal? q.val:p.val;
return lowestCommonAncestorHelper(root, largerVal, smallerVal);
}

private TreeNode lowestCommonAncestorHelper(TreeNode root, int largerInput, int smallerInput){
if(smallerInput > root.val)
return lowestCommonAncestorHelper(root.right, largerInput, smallerInput);
else if(largerInput < root.val)
return lowestCommonAncestorHelper(root.left, largerInput, smallerInput);
else
return root;
}
}

• 本文已收录于以下专栏：

## 235. Lowest Common Ancestor of a Binary Search Tree [easy] (Python)

• coder_orz
• 2016年05月25日 15:26
• 888

## leetcode 235: Lowest Common Ancestor of a Binary Search Tree

leetcode 235: Lowest Common Ancestor of a Binary Search Tree python, java, c++
• xudli
• 2015年07月11日 06:34
• 10634

## [Java]LeetCode235 Lowest Common Ancestor of a Binary Search Tree

[Java]LeetCode235 Lowest Common Ancestor of a Binary Search Tree
• fumier
• 2015年08月22日 21:31
• 735

## LeetCode 235: Lowest Common Ancestor of a Binary Search Tree

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS...
• sunao2002002
• 2015年07月27日 18:41
• 2164

## [LeetCode 236] Lowest Common Ancestor of a Binary Tree

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accord...
• sbitswc
• 2015年08月25日 13:31
• 2438

## leetcode 236: Lowest Common Ancestor of a Binary Tree

leetcode 236: Lowest Common Ancestor of a Binary Tree PYTHON, JAVA, C++
• xudli
• 2015年07月14日 02:35
• 6343

## [Java]Leetcode236 Lowest Common Ancestor of a Binary Tree

• fumier
• 2015年08月23日 08:58
• 853

## LeetCode 236. Lowest Common Ancestor of a Binary Tree（二叉树的最低公共祖先）

• jmspan
• 2016年04月06日 06:43
• 588

## leetcode 236. Lowest Common Ancestor of a Binary Tree-二叉树共同祖先|深度遍历|递归|非递归

• happyaaaaaaaaaaa
• 2016年06月11日 11:44
• 688

## P2P点播系统性能优化（3）: Tracker服务器选择最优节点

• checo_chua
• 2011年08月17日 14:52
• 666

举报原因： 您举报文章：Lowest Common Ancestor of a Binary Search Tree 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)