[LC] 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]
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return root;
}
if (root.val > p.val && root.val > q.val) {
return lowestCommonAncestor(root.left, p, q);
} else if (root.val < p.val && root.val < q.val) {
return lowestCommonAncestor(root.right, p, q);
} else {
return root;
}
}
}
[LC] 235. Lowest Common Ancestor of a Binary Search Tree的更多相关文章
- 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 二叉搜索树的最小共同父节点
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 解题报告(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 ...
- (easy)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 ...
随机推荐
- 106.HttpResponse对象详解
HttpResponse对象 Django服务器接收到客户端发送过来的请求之后,会将提交上来的这些数据封装成一个HttpResquest对象传给视图函数.那么视图函数在处理完成相关的逻辑后,也需要返回 ...
- ModernRNN
GRU RNN存在的问题:梯度较容易出现衰减或爆炸(BPTT) ⻔控循环神经⽹络:捕捉时间序列中时间步距离较⼤的依赖关系 RNN: \[ H_{t} = ϕ(X_{t}W_{xh} + H_{t-1} ...
- Asp.Net MVC主项目访问不到分离项目控制器的解决方案
我在portal主项目外新建一个分离项目,控制器和Model都写在分离项目中,视图层写在portal中. 我更改了命名空间,引用了Dll,还是不能访问到控制器. 找到问题: 最后我发现是主项目port ...
- ZJNU 1528 - War--高级
类似于1213取水 可以把空投当作第0个城市 最后将0~n的所有城市跑最小生成树 /* Written By StelaYuri */ #include<iostream> #includ ...
- CF633H Fibonacci-ish II(莫队+线段树)
温馨提示:本题十分卡常数,我手动开O2才过的.而数据范围不伦不类的n<=30000,常数小的O(n2)居然比O(n√nlogn)跑得快…… 考虑插进去一个元素对答案产生的影响.原本数列为Σa[i ...
- 01 语言基础+高级:1-4 接口与多态_day10【接口、多态】&&day11【final、匿名内部类】
day10[接口.多态] 接口三大特征——多态引用类型转换 教学目标写出定义接口的格式写出实现接口的格式说出接口中成员的特点能够说出使用多态的前提条件理解多态的向上转型理解多态的向下转型 day10_ ...
- C# 创建、部署和调用WebService的简单示例 (转)
C# 创建.部署和调用WebService的简单示例(转) 转自 https://www.cnblogs.com/Brambling/p/7266482.html webservice 可以用于分 ...
- 分辨率单位及换算,LW / PH、LP / mm、L / mm、Cycles / mm、Cycles / pixel、LP / PH
对于测试数字成像设备非常重要的参数是分辨率.但有不同的方式来表达数码相机的分辨率,有时令人困惑.下面介绍的是是最常见的单位. 百万像素 “这台相机有1000万像素的分辨率”是我们经常在广告上看到的数据 ...
- 【网络流】Modular Production Line
[网络流]Modular Production Line 焦作上的一道,网络流24题中的原题.... https://nanti.jisuanke.com/t/31715 给出了1e5个点,但是因为最 ...
- LeetCode No.115,116,117
No.115 NumDistinct 不同的子序列 题目 给定一个字符串 S 和一个字符串 T,计算在 S 的子序列中 T 出现的个数. 一个字符串的一个子序列是指,通过删除一些(也可以不删除)字符且 ...