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 ...
随机推荐
- Python:time模块&序列化&生成随机数&反射
time模块:>>> import time >>> time.time <built-in function time> >>> t ...
- Python使用中文注释和输出中文(原创)
刚开始学习python,需要在Python中注释中文和输出中文,现在开始尝试: 仅为初步学习参考,高手请绕行. -------------------------------------------- ...
- HttpServletrequest 与HttpServletResponse总结
如果说DOM是javascript与HTML的桥梁,那么servlet就是前端与后端的桥梁,HttpServletRequest和HttpServletResponse就是之间的信使,好了,废话不多说 ...
- CityEngine中动态水的实现
地址:http://pan.baidu.com/share/link?shareid=3871210059&uk=3492170216 密码:am5b 在今年Esri全球用户大会和Esri中国 ...
- HTML5的viewport使用
viewport 语法介绍: <!-- html document --> <meta name="viewport" content=" height ...
- C++调用动态库中的虚基类成员函数时总是进错函数
原创文章,转载请注明作者与本文原始URL. 问题描述:最近遇到这样一个问题,在调用C++的一个成员函数时,总是进错函数.在调用 pMsg->GetMsgContent() 的时候,总是进入到 p ...
- php实现验证码
验证码在表单实现越来越多了,但是用js的写的验证码,总觉得不方便,所以学习了下php实现的验证码.好吧,其实是没有事情干,但是又不想浪费时间,所以学习了下php实现验证码.正所谓,技多不压身.而且 ...
- 高效快捷解决一个TextView显示多种字体的控件SpannableTextView
这个控件本人强烈推荐,它会使得布局非常的简单且高效: 下面这个布局如果是你,你会用多少层?多少控件生成? 告诉你吧,一个SpannableTextView控件就搞定了! 它把TextView和Span ...
- opengles tutorial
https://developer.apple.com/library/ios/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide ...
- OWIN的理解和实践(一) – 解耦,协作和开放
概述 OWIN的全称是Open Web Interface For .Net, 是MS在VS2013期间引入的全新的概念, 网上已经有不少的关于它的信息, 这里我就谈下我自己的理解: OWIN是一种规 ...