[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

Problem DescriptionGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two gi...

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

LeetCode 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树

235. Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the low...

LeetCode 235 Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最小公共祖先)

翻译给定一个二叉搜索树(BST),找出两个给定节点的最小公共祖先(LCA)。根据维基百科对于LCA的定义:“最小公共祖先的定义是对于两个节点v和s有一个最小的节点T, 以至于v和s都是T的后代(其中我...
  • NoMasp
  • NoMasp
  • 2016年01月12日 19:21
  • 1943

Leetcode:235. Lowest Common Ancestor of a Binary Search Tree(JAVA)

【问题描述】 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nod...

leetcode-235-Lowest Common Ancestor of a Binary Search Tree

Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowes...

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 th...

LeetCode-235. Lowest Common Ancestor of a Binary Search Tree (Java)

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS...
  • NOadu
  • NOadu
  • 2017年07月13日 12:44
  • 114

【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree

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

leetcode235. Lowest Common Ancestor of a Binary Search Tree

235. Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode] 235. Lowest Common Ancestor of a Binary Search Tree
举报原因:
原因补充:

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