[刷题] 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 ...
随机推荐
- .Net5 WPF快速入门系列教程
一.概要 在工作中大家会遇到需要学习新的技术或者临时被抽调到新的项目当中进行开发.通常这样的情况比较紧急没有那么多的时间去看书学习.所以这里向wpf技术栈的开发者分享一套wpf教程,基于.net5框架 ...
- 201871030118-雷云云 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告
项目 内容 课程班级博客 班级链接 这个作业要求链接 作业链接 我的课程学习目标 (1)体验软件项目开发中的两人合作,练习结对编程(2)掌握Github协作开发程序的操作方法(3)学习遗传算法 这个作 ...
- Unity2D项目-平台、解谜、战斗! 1.1战斗底层组件CanFight-CanBeFighted
各位看官老爷们,这里是RuaiRuai工作室,一个做单机游戏的兴趣作坊. 本文对该2D项目中战斗底层组件的开发及设计思路做一个总结,希望各路同行多多交流,各路大佬多多指点. 实例特征分析 首先对于各个 ...
- Java(56-64)【IDEA】
1.IDEA的使用 集成开发软件 eclipse IDEA 第一步:创建新的项目 第二步:空白的project 第三步:modules->java 写在src中 src->package- ...
- 学习WEB前端是应该自学还是参加培训机构?
先说观点,我强烈建议每个人都要自学,不要参加培训班. 我干web前端工程师这个职位已经有6年多的时间,之前在蚂蚁金服做过2年,后来离开是因为加班实在熬不住才走的,像这些已经上市的互联网公司几乎没有不加 ...
- Day01_01_java背景
1.Java背景 --由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台的总称. --Java语言是一种可以撰写跨平台应用软件的面向对象的程序设计语言,由当 ...
- Day09_47_Map
Map 集合 Map集合** 继承关系** <interface>: Map(接口)<---HashMap(class)/HashTable(class)/SortedMap(子接口 ...
- aws EKS
登陆aws账号 1)找到eks 相关的项目,并进入 2)填写集群的名称,然后下一步 3)集群设置页面,添加集群服务角色 (aws eks cluster role) 4)继续集群配置 5)集群创建完成 ...
- 【Spring】 Spring如何解决循环依赖的问题?
https://mp.weixin.qq.com/s/FtbzTMxHgzL0G1R2pSlh-A 通常来说,如果问Spring内部如何解决循环依赖,一定是单默认的单例Bean中,属性互相引用的场景. ...
- Python 使用xlsxwriter绘制Excel表格
最近在统计资产,正好看到了xlsxwriter这个表格生成模块,借此机会,熟悉一下,写点有趣的小案例,一开始想使用C++ QT图形化开发一套自动化运维平台,但后来发现不仅消耗时间而且需要解决QT Qs ...