【leetcode❤python】235. Lowest Common Ancestor of a Binary Search Tree
#-*- coding: UTF-8 -*-
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
maxp=p if p.val>q.val else q
minp=q if q.val<p.val else p
print maxp.val,minp.val
if maxp.val>=root.val and minp.val<=root.val:
return root.val
elif minp.val>root.val:
return self.lowestCommonAncestor(root.right,maxp,minp)
elif maxp.val<root.val:
return self.lowestCommonAncestor(root.left,maxp,minp)
【leetcode❤python】235. Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- [LeetCode&Python] Problem 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 ...
- 【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 ...
- 【LeetCode】235. Lowest Common Ancestor of a Binary Search Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leet ...
- 【一天一道LeetCode】#235. Lowest Common Ancestor of a Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【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 ...
- 【easy】235. Lowest Common Ancestor of a Binary Search Tree
题意大概是,找两个节点的最低公共祖先 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod ...
- 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 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [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 ...
随机推荐
- zw版【转发·台湾nvp系列Delphi例程】HALCON AffineTransImage
zw版[转发·台湾nvp系列Delphi例程]HALCON AffineTransImage unit Unit1;interfaceuses Windows, Messages, SysUtils, ...
- yii2顶部导航使用
yii2中使用顶部导航的具体方法: 1.视图中调用两个类: use yii\bootstrap\Nav;use yii\bootstrap\NavBar; 2. <?php ...
- keep your work after network broken
如下设置可以保证在网络中断后tso仍然可以继续active,并等到下一次reconnect sys1.tcpparms(tnprof7d) -- Dataset name and PDS name b ...
- ubuntu android环境配置
1.下载eclipse 2.下载sdk 3.安装adt 4.配置sdk环境路径sudo gedit /etc/profile export PATH=$JAVA_HOME/bin:$JAVA_HOME ...
- dataTabel转成dataview插入列后排序
if (!string.IsNullOrEmpty(strQuyu) && !string.IsNullOrEmpty(strZuhao)) { string[] param = { ...
- struts2 笔记02 文件上传、文件下载、类型转换器、国际化的支持
Struts2的上传 1. Struts2默认采用了apache commons-fileupload 2. Struts2支持三种类型的上传组件 3. 需要引入commons-fileupload ...
- echo date
echo -n 不换行 echo -n '123';echo '456' #结果:123456 echo -e 启用下列反斜杠转义的解释 \b 退格 \t制表 \n 换行 \r 回车 echo -e ...
- 在 mysql 中利用 Duplicate key, 一句话实现存在的更新不存在插入功能
mysql 中可以用一个sql命令实现在插入时,如果发现唯一索引重复的记录则自动改为更新语句, 语句如下: '; 注意,radcheck 表中 username 和 attribute 列是个组合的唯 ...
- Apache服务器访问过慢分析及解决
起因:线上的一台服务器,最近总是出现 访问 很慢的情况发生,点击一个链接要2秒钟以上才能打开,按照我们对于访问人数的估计,服务器应该不至于响应这么慢,从而需要针对这个问题进行分析,来解决网站访问过慢. ...
- 用Jquery控制文本框只能输入数字和字母
用Jquery控制文本框只能输入数字和字母 $.fn.onlyNum = function () { $(this).keypress(function (event) { var eventObj ...