LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)
题意:
给一棵二叉排序树,找p和q的LCA。
思路:
给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小。根据这个特性,找LCA就简单多了。
分三种情况:
(1)p和q都在root左边,那么往root左子树递归。
(2)在右同理。
(3)一左一右的,那么root->val肯定大于其中的1个,小于另一个。
/**
* 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) { //这是一棵排序树啊!
if( (root->val - p->val ) * (root->val - q->val) <= ) //一大一小必会产生负的,或者root为其中1个,那么就是0
return root;
if( max(p->val, q->val) < root->val ) //都在左边
return lowestCommonAncestor(root->left, p, q);
else //都在右边
return lowestCommonAncestor(root->right, p, q);
}
};
AC代码
python3
迭代
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
while root:
if (p.val-root.val)*(q.val-root.val)<=0: #一左一右
return root
elif p.val<root.val: #左
root=root.left
else:
root=root.right
AC代码
递归
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
if (p.val-root.val)*(q.val-root.val)<=0: #一左一右
return root
elif p.val<root.val: #左
return self.lowestCommonAncestor(root.left, p, q)
else:
return self.lowestCommonAncestor(root.right, p, q)
AC代码
LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)的更多相关文章
- 235. Lowest Common Ancestor of a Binary Search Tree(LCA最低公共祖先)
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the ...
- [LeetCode] 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 ...
- LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告
https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...
- [LeetCode]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 ...
- LeetCode——Lowest Common Ancestor of a Binary Search Tree
Description: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given no ...
- Python3解leetcode 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 ...
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree (2 solutions)
Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest com ...
随机推荐
- mysql_sql语句之美
无线地址及数量统计 单个用户无线登录信息统计
- google其他入口地址
http://178.45.251.74/ http://64.233.166.51/ http://61.19.1.117/ http://64.233.166.42/ http://61.19.1 ...
- CentOS 下 Codeblocks 的 安装 + 汉化 以及 基本使用介绍
Codeblocks 安装 注:在root用户下运行下列命令 1.安装gcc,需要c和c++两部分,默认安装下,CentOS不安装编译器的,在终端输入以下命令即可 yum install gcc yu ...
- FPGA串口波特率简析
以前用单片机,一直都是直接用就行,设置波特率时,直接写9600就行,一直没有仔细考虑过,今天打算用FPGA写个串口程序时才知道,原来根本就是没弄明白.一下是我的一些见解.如果诸位看官觉得不对,欢迎指正 ...
- C#向C++编写的DLL传递字符串参数的办法
使用StringBuilder,举例: C++代码中函数定义如下: PVPOWERFORCASTDLL_API int PVPowerForcast(SForcastInfo &_Forcas ...
- docker下PHP+Nginx+HHVM运行环境
Dockerfile 准备开始,我们创建一个 Dockerfile —— Dockerfile 包含如何创建所需镜像的指令. FROM centos:centos6MAINTAINER Mike ...
- Cube and EarthDistance
前言:项目中用到了postgreSQL中的earthdistance()函数功能计算地球上两点之间的距离,中文的资料太少了,我找到了一篇英文的.讲的很好的文章 ,特此翻译,希望能够帮助到以后用到ear ...
- Asp.Net MVC过滤器小试牛刀
在上学期间学习的Asp.Net MVC,基本只是大概马马虎虎的了解,基本处于知其然而不知其所以然.现在到上班,接触到真实的项目,才发现还不够用,于是从最简单的过滤器开始学习.不得不说MVC的过滤器真是 ...
- ExtJs 4.2 treePanel 点击树节点 传送参数到后台(多个参数)
//***********************************************左边树开始********************************************** ...
- Hibernate从入门到精通(五)一对一单向关联映射
上次的博文中Hibernate从入门到精通(四)基本映射我们已经讲解了一下基本映射和相关概念,接下来我们会讲稍微复杂点的映射——关系映射. 关系映射分类 关系映射即在基本映射的基础上处理多个相关对象和 ...