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

相关文章推荐

Leet Code OJ 235. Lowest Common Ancestor of a Binary Search Tree [Difficulty: Easy]

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

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

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

求二叉树的LCA,也就是两个节点的最低公共祖先。

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

235. Lowest Common Ancestor of a Binary Search Tree

题目:Lowest Common Ancestor of a Binary Search Tree原题链接:https://leetcode.com/problems/lowest-common-an...

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

235. Lowest Common Ancestor of a Binary Search Tree

leetcode 235 - Easy

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

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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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