题意:

  给一棵二叉排序树,找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最近公共祖先)的更多相关文章

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

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

  3. LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告

    https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...

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

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

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

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

  8. 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 利用二 ...

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

随机推荐

  1. unity3d应用内分享(微信、微博等)的实现

    问题:如何在unity3d的游戏中实现分享功能,如图 思路: 1.分享功能的实现方式有多种,较方便快捷的一种是直接调用android的API来调出系统的分享界面 2.unity3d里面调用androi ...

  2. (转)linux性能优化总结

    感谢博客http://sillycat.iteye.com提供的资料 linux性能检查(一)介绍和CPU 通常监控的子系统有: CPU Memory IO Network 应用类型 IO相关,处理大 ...

  3. 关于CSS的图像放大问题的解决,需要借助jQuery等直接用css3设置

    W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发, ...

  4. 利用PHPRPC以及SOAP分别实现PHP的Webserver功能

    服务端:phprpc服务端 <?php /* phprpc 服务端演示 * time:2014-06-23 */ require_once 'phprpc_server.php';//引入服务端 ...

  5. 2016 系统设计第一期 (档案一)MVC a标签 跳转 Html.ActionLink的用法

    html: <a class="J_menuItem" href="baidu.com">权限管理</a> cshtml: 原有样式: ...

  6. Maven系列--"maven-compiler-plugin"的使用、Maven之Surefire插件

    一."maven-compiler-plugin"的使用 http://my.oschina.net/poorzerg/blog/206856 二.Maven之Surefire插件 ...

  7. Why are very few schools involved in deep learning research? Why are they still hooked on to Bayesian methods?

    Why are very few schools involved in deep learning research? Why are they still hooked on to Bayesia ...

  8. 解决WIN8 磁盘100 活动占用100% win8硬盘一直响

    一.先看最终效果: 二.再说解决办法:   1.任务管理器关闭进程 taskhost.exe和类似于taskhostxx.exe开头的进程. 2.在电源管理里面设置2分钟不使用硬盘则关闭硬盘,看我的截 ...

  9. uva 11039

    水题  排序 判符号 #include <cstdio> #include <cstring> #include <algorithm> using namespa ...

  10. CSU1326+背包+并查集

    先预处理出有多少个任务即可 #include<stdio.h> #include<stdlib.h> #include<string.h> #include< ...