[leetcode] 235. Lowest Common Ancestor of a Binary Search Tree

原创 2016年06月01日 12:23:34

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
  • xudli
  • 2015年07月11日 06:34
  • 10632

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

题目链接https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/题目原文 Given a bina...
  • coder_orz
  • 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
  • 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
  • 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
  • sbitswc
  • 2015年08月25日 13:31
  • 2432

[Java]Leetcode236 Lowest Common Ancestor of a Binary Tree

求二叉树的公共父结点
  • fumier
  • fumier
  • 2015年08月23日 08:58
  • 853

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

原题网址:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, f...
  • jmspan
  • jmspan
  • 2016年04月06日 06:43
  • 588

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

原题链接:236. Lowest Common Ancestor of a Binary Tree 【思路-Java】dfs|递归实现 本题是二叉树的深度遍历的典型应用,基础还是二叉树的遍历。 ...
  • happyaaaaaaaaaaa
  • 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
  • xudli
  • 2015年07月14日 02:35
  • 6343

P2P点播系统性能优化(3): Tracker服务器选择最优节点

在集中式的P2P网络系统中,Tracker服务器的主要功能是提供最优节点检索功能 ;Tracker服务器的最优节点选择算法如下:                                1....
  • checo_chua
  • checo_chua
  • 2011年08月17日 14:52
  • 663
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode] 235. Lowest Common Ancestor of a Binary Search Tree
举报原因:
原因补充:

(最多只允许输入30个字)