Easy-题目14:235.Lowest Common Ancestor of a Binary Search Tree

原创 2016年05月30日 19:57:06

题目原文:
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.
题目大意:
给出一个二叉排序树,求两个节点的最近公共祖先。(一个节点的祖先也可以是自己)
题目分析:
(1) 空节点的祖先是空;
(2) 若当前根节点的值比两个节点的值都大,则公共祖先在左子树里,递归向左子树搜索;
(3) 若当前根节点的值比两个节点的值都小,则公共祖先在右子树里,递归向右子树搜索;
(4) 上述情况均不成立,则当前节点就是公共祖先。
源码:(language:c)

struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
    if (!root || !p || !q)           
        return NULL;
    else if (root->val < p->val && root->val < q->val)
        return lowestCommonAncestor(root->right, p, q);
    else if (root->val > p->val && root->val > q->val)
        return lowestCommonAncestor(root->left, p, q);
    else
        return root;        
}

成绩:
24ms,beats 22.03%,众数24ms,77.40%
Cmershen的碎碎念:
注意利用二叉排序树的定义的性质。总结如下:
二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:
(1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2)若右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3)左、右子树也分别为二叉排序树;
(4)没有键值相等的节点。
性质:二叉排序树的中序遍历是递增序列。

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

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
  • 887

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
  • 735

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

[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
  • 2438

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

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...
  • HWHuangeian
  • HWHuangeian
  • 2015年10月08日 18:59
  • 176

[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
  • 688
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Easy-题目14:235.Lowest Common Ancestor of a Binary Search Tree
举报原因:
原因补充:

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