[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 BST.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Given binary search tree: root = [6,2,8,0,4,7,9,null,null,3,5]

Example 1:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output: 6
Explanation: The LCA of nodes2and8is6.
Example 2:
Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output: 2
Explanation: The LCA of nodes2and4is2, since a node can be a descendant of itself according to the LCA definition.
Note:
- All of the nodes' values will be unique.
- p and q are different and both values will exist in the BST.
# 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
"""
p_val=p.val
q_val=q.val if p_val<root.val and q_val<root.val:
return self.lowestCommonAncestor(root.left,p,q)
elif p_val>root.val and q_val>root.val:
return self.lowestCommonAncestor(root.right,p,q)
else:
return root
[LeetCode&Python] Problem 235. Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- 【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 ...
- 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 ...
- 【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 二叉搜索树的最小共同父节点
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 ...
- [刷题] 235 Lowest Common Ancestor of a Binary Search Tree
要求 给定一棵二分搜索树和两个节点,寻找这两个节点的最近公共祖先 示例 2和8的最近公共祖先是6 2和4的最近公共祖先是2 思路 p q<node node<p q p<=node& ...
- 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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- lumion的物体系统5.30
1.“自然"点击这棵树.可以打开自然库.不同的植物分类有很多页数. 选择一棵树,自动返回页面单击鼠标左键可以种植这个树.成排种树:点击”人群安置“点击我们想安置的起点.再点击终点.用鼠标右键 ...
- 用VS2017编写C语言的Hello World
1.新建项目 2.选择新建空项目 3.在源文件处右键单击,选择添加-新建项 4.选择“c++文件”,将名称后缀改成.c即可用C语言编写程序 5.编写代码: #include <stdio.h&g ...
- validation-api各注解的用法
入参用@Valid,要不下面实体类中的注解不生效 @AssertFalse 被注解的元素必须为false@AssertTrue 被注解的元素必须为True@DecimalMax(value) 被注解的 ...
- Java中的异常处理与抛出
一.异常处理 程序运行过程中出现的,导致程序无法继续运行的错误叫做异常. Java中有多种异常,所有异常的父类是Throwable,他主要有两个子类Error和Exception. Error一般是J ...
- vBox Arch UEFI LVM安装
Table of Contents 介绍 配置 基础 VirtualBox配置 安装准备 基础 分区 格式化 挂载 安装 选择镜像 安装基本系统 配置 fstab chroot 一些配置 lvm2 网 ...
- FastReport预览后直接邮件发送
- STL中set的使用方法
第一次想认真地学学set,是在我做一题treap的时候产生的念头.(HNOI2004 宠物收养场,洛谷P2286) 嗯,虽然学过一丢丢的treap和splay,但是这编程复杂度貌似有点高…… 无奈翻开 ...
- Java oop(一些自己的理解,并没有展开很细)
一下内容是自己总结用的,只是按照自己的理解去写.参考的是菜鸟教程.Java 是一个面向对象的语言.OOP就是面向对象编程.封装:在某些类里面,某些属性不想向外暴露,但是我们又想提供一个方法去访问或修改 ...
- 字符串及其操作,字符的Unicode编码
plainText=input('message:') for c in plainText: print(chr(ord(c)-3),end='') plainText=input('message ...
- cocos图片的选择以及压缩
我们在使用cocos在windows平台下,运行速度很快很流畅,很强大,可是当我们打包成apk文件,在手机上运行的时候,流畅度很可能降低,甚至还有间歇性内存彪高. 游戏内存优化我们一般可以从这么3个方 ...