[刷题] 235 Lowest Common Ancestor of a Binary Search Tree
要求
- 给定一棵二分搜索树和两个节点,寻找这两个节点的最近公共祖先
示例
- 2和8的最近公共祖先是6
- 2和4的最近公共祖先是2

思路
- p q<node
- node<p q
- p<=node<=q
实现
1 class Solution {
2 public:
3 TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
4
5 assert( p != NULL && q != NULL );
6
7 if( root == NULL )
8 return NULL;
9
10 if( p->val < root->val && q->val < root->val )
11 return lowestCommanAncestor( root->left , p , q );
12 if( p->val > root->val && q->val > root->val )
13 return lowestCommanAncestor( root->right , p , q );
14
15 return root;
16 }
17 };
相关
- 98 Validate Binary Search Tree
- 450 Delete Node in a BST
- 108 Convert Sorted Array to Binary Search Tree
- 230 Kth Smallest Element in a BST
- 236 Lowest Common Ancestor of a Binary Tree
[刷题] 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 解题报告(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 th ...
- 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 ...
随机推荐
- 学习笔记-git 上传
0.git add * (如果你需要修改源码需要在 1 之前使用,然后再回到 1) 1.git commit -m '提交文字描述' 2.git push -u origin master (上传到主 ...
- OOUnit3Summary
一.JML基础梳理及工具链 jml语言基础 JML的全称是Java Modeling language,是一种行为接口规格语言,通过JML及其支持工具,不仅可以基于规格自动构造测试用例,还可用SMT ...
- inline®ister
inline关键字: 内联只是一个请求,不代表编译器会响应:同时某些编译器会将一些函数优化成为内联函数. C++在类内定义的函数默认是内联函数,具体是否真变成内联函数还需看编译器本身. registe ...
- aws EKS
登陆aws账号 1)找到eks 相关的项目,并进入 2)填写集群的名称,然后下一步 3)集群设置页面,添加集群服务角色 (aws eks cluster role) 4)继续集群配置 5)集群创建完成 ...
- 按照自己的思路去研究Spring AOP源码【1】
目录 一个例子 Spring AOP 原理 从@EnableAspectJAutoProxy注解入手 什么时候会创建代理对象? 方法执行时怎么实现拦截的? 总结 问题 参考 一个例子 // 定义一个切 ...
- 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
Given an array of integers nums and an integer limit, return the size of the longest continuous suba ...
- 从UWP项目移植到WinUI桌面版你需要做哪些事情
就像文章标题说的我是打算写一篇从UWP移植到WinUI的帖子,本来打算是想写一篇WinUI的学习帖子,可是觉得市面上UWP的教程WPF的教程都是很多了,所以干脆就直接硬怼项目吧,先声明我不是来挖UWP ...
- html书签展示(带搜索)
源代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...
- css3动画大全
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Webuploader 简单图片上传 支持多图上传
简介: 通过webuploader 实现简单的图片上传功能,支持多张图上传 官方文档传送门:http://fex.baidu.com/webuploader/getting-started.html# ...