# [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 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.

Solution

Idea: the value of lowest common ancestor is between the values of input two nodes. Use recursive function to find it.

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
int pval = p->val;
int qval = q->val;
if (root->val==pval || root->val==qval)
return root;
else if (min(pval,qval)<root->val && max(pval,qval)>root->val)
return root;
else if (root->val>max(pval,qval))
return lowestCommonAncestor(root->left,p,q);
else if (root->val<min(pval,qval))
return lowestCommonAncestor(root->right,p,q);
return root;
}
};

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

## 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
• 10632

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

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

## 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

## [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
• 734

## [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
• 2432

## [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
• 687

## 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

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

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

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