Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树
给定一个二叉搜索树的两个节点,找出他们的最近公共祖先,如,
_______6______
/ \
___2__ ___8__
/ \ / \
0 4 7 9
/ \
3 5 2和8的最近公共祖先是6,2和4的最近公共祖先是2,假设找的3和5
TreeNode* l =lowestCommonAncestor(root->left,p,q) ;
TreeNode* r =lowestCommonAncestor(root->right,p,q) ;
返回到4时两个都不是Nullptr,那么要返回4的指针 即if(l && r ) return root;
返回到2时只有r是Nullptr,那么要返回4的指针 即else if(!l && r) return r;
返回到6时只有l是Nullptr,那么要返回4的指针 即else if(l && !r) return l;
返回到8时都是是Nullptr,那么返回NULL 即else return NULL;
本文的求解方法没有利用二叉搜索树的特点,因此效率较低
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root) return NULL;
else if(!q&&!p){
return NULL;
}
else if(root->val == p->val){
return root;
}
else if(q->val == root->val){
return root;
}
else {
TreeNode* l =lowestCommonAncestor(root->left,p,q) ;
TreeNode* r =lowestCommonAncestor(root->right,p,q) ;
if(l && r ) return root;
else if(!l && r) return r;
else if(l && !r) return l;
else return NULL;
}
}
};
Leetcode 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 二叉搜索树的最近公共祖先
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 ...
- 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 ...
- Java [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 ...
- Java for LeetCode 235 Lowest Common Ancestor of a Binary Search Tree
递归实现如下: public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(p.val>ro ...
- 【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 ...
随机推荐
- Daily Scrum 12.12
今日完成任务: 解决文档上传时TagAssociation的建立异常问题:解决问题页面标签点击卡死的BUG. 发现问题: 文档下载量浏览量显示不正确: 文档打开时全都是同一个PDF: 右侧最佳资源的显 ...
- sprintf()函数基本用法
基本用法 sprintf 是个变参函数,定义如下: int sprintf( char *buffer, const char *format [, argument] ... ); sprintf的 ...
- <T>的用法
///类型转换 template <class T1,class T2> vector<T2> ObjcetsSwap(vector<T1> objects) { ...
- mybatis mapper.xml 配置文件问题(有的错误xml是不报的) 导致服务无法启动 。
转载自 开源编程 一舟mybatsi xml编译报错,tomcat启动一直循环,导致内存溢出,启动失败 mapper.xml怎么知道有没有编译错误,哪个位置有错误 这应该是mybatis的一个bug, ...
- (Python) 函数、匿名函数
本节将学习如何用Python定义函数,调用函数,以及学习匿名函数的使用 1.定义函数 Python中定义函数用关键字def,如下例所示,testFun为函数名 def testFun(): print ...
- 如何让chrome始终运行插件
使用chrome可能有时候会拦截比如阿里旺旺和腾讯等的登录插件,那么怎么才始终允许,而不需要每次确认呢.下面. 1. 打开Chrome浏览器. 在地址栏中输入 chrome://plugins 回车 ...
- 【转载】-- vi/vim使用
vi/vim 基本使用方法本文介绍了vi (vim)的基本使用方法,但对于普通用户来说基本上够了!i/vim的区别简单点来说,它们都是多模式编辑器,不同的是vim 是vi的升级版本,它不仅兼容vi的所 ...
- linux增加根分区大小
以下操作以root身份运行 1.增加一个新分区(从原有硬盘分,或增加一个新硬盘并进行分区fdisk) 格式化成ext4(mkfs.ext4 /dev/sdb1,假设为/dev/sdb1) 2.将新的 ...
- JAVA学习博客---2015.5
上一次的学习博客写的和流水账差不多,有点生硬的和背目录一样,所以既然学习的目的是程序,那么这个月的学习博客就用程序来说点东西吧.这个月看了一些C和JAVA的视频,开始看别人写的程序,能看的懂但是自己去 ...
- ASP.NET MVC请求处理管道生命周期的19个关键环节(13-19)
在上一篇"ASP.NET MVC请求处理管道生命周期的19个关键环节(7-12) ",体验了7-12关键环节,本篇继续. ⒀当请求到达UrlRoutingModule的时候,Url ...