Leetcode 235 Lowest Common Ancestor of a Binary Search Tree 二叉树

时间:2022-09-02 08:20:21

给定一个二叉搜索树的两个节点,找出他们的最近公共祖先,如,

        _______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 二叉树的更多相关文章

  1. 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 利用二 ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. (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 ...

  7. 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 ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. git 修改最后一次提交的用户名 或者 commit的内容

    修改git最后一次提交的命令 $ git commit --amend 修改git最后一次提交用户名的相关命令 git config user.name 'wangz' git config user ...

  2. [Ubuntu] Linux下使用google app engine,无法打开https网站的解决方法

    为什么这里写的是 google app engine?原因我就不解释了.步骤如下: 1)安装证书导入工具:$ sudo apt-get install libnss3-tools 2)导入CA.crt ...

  3. Windbg源码调试

    Windbg提供比VS2008丰富很多的调试命令,尤其是调试多线程程序. 今天试着怎么使用源代码方式调试.为了说明调试命令,<C++标准库>一书里的例子做示范. // testcast.c ...

  4. backbone案例

    http://www.kuqin.com/webpagedesign/20120807/324101.html http://udonmai.com/code/todos-backbone%E6%A1 ...

  5. TCP服务器:多进程

    代码: server: #include<netinet/in.h> #include<sys/socket.h> #include<sys/wait.h> #in ...

  6. POJ 3991 Seinfeld

    首先进行一次括号匹配,把正确匹配的全部删去. 删去之后剩下的状态肯定是 L个连续右括号+R个连续左括号. 如果L是偶数,答案是L/2+R/2: 否则答案是 (L-1)/2+(R-1)/2+2: #in ...

  7. Volley源码学习笔记

    标签(空格分隔): Volley 创建RequestQueue 使用Volley的时候,我们首先需要创建一个RequestQueue对象,用于添加各种请求,创建的方法是Volley.newReques ...

  8. Android艺术——探究Handler运行机制

    我们从开发的角度来说,Handler是Android 的消息机制的上层接口.说到Handler,大家都会说:哦,Handler这个我知道干什么的,更新UI.没错,Handler的确是用于更新UI的,具 ...

  9. 配置python3

    1. 下载解压.$ wget https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tgz$ tar zxvf Python-3.4.1.tgz 2 ...

  10. vue的单向数据流

    父级向子组件传递的值, 子组件不能直接修改这个穿过来的值,否则会曝出警告,这就是单项数据流. 如果是引用值,传递的是引用值得地址,而不是值本身,也就是说,子组件里修改这个传过来的值,通常的做法是放到它 ...