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

        _______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. golang--gopher北京大会(2)(rework)

    三.七牛老许 qlang: github qiniu/qlang microservice architecture: http://martinfowler.com/articles/microse ...

  2. swift webView 提出这样的要求你能忍吗?

    提出的要求是:webView字体显示大小合适,里面会有大的图片,大的表格,不能按比例缩放,可以左右滑动查看. 首先,如果要满足上面的要求需要将webView放入scrollView中显示 scroll ...

  3. C# webApi 与 AngularJs 实现增删改Demo 讲解(一)

    公司在使用webAPI+AngularJs+SlcikGrid进行产品开发,自己也是初学Angular,就做了一个Demo,实现增删改功能,希望可以帮助大家. 界面如同所示:  数据库一张单表很简单, ...

  4. ArcGIS删除部分数据后全图范围不正确

      我有一个全国地图的图层,现在删除图层中其他省份,只保留山东省的图形,但是点击全图后,全图范围仍然是全国地图时候的全图范围,使用的版本是ArcGIS9.3,数据存放在9.3的个人数据库中(Perso ...

  5. Pojo类(plain ordinary java object)

    POJO有一些private的参数作为对象的属性.然后针对每个参数定义了get和set方法作为访问的接口.例如:public class User {private long id;private S ...

  6. 一个自己用的代码备份工具,支持delphi,android,java,可以自己添加配置,灵活支持大部分编程语言

    下面就是这个软件的下载地址 http://files.cnblogs.com/files/stevenlaz/release.zip 绿色软件 MConfig.exe 进行安装,或者卸载 MBacku ...

  7. POJ Ant Counting DP

    dp[i][j]表示前i种蚂蚁组成元素个数为j的集合有多少种. 则dp[i][j] = dp[i-1][j] + dp[i-1][j-1] + ... + dp[i-1][ max(0,j-a[i]) ...

  8. Maven学习之 Settings

    虽然天天在用,但是没有系统的学习过,总觉得别扭. 只能用于Java项目. 约定: repository  翻译成 仓库 build 翻译成 构建 build system 翻译成 构建系统 build ...

  9. Floyd 求最短路(poj 1161)

    Floyd-Warshall算法介绍: Floyd-Warshall算法的原理是动态规划. 设为从到的只以集合中的节点为中间节点的最短路径的长度. 若最短路径经过点k,则: 若最短路径不经过点k,则. ...

  10. Ubuntu下配置L2TP

    发现PPTP已经不可用了,不知是不是又被墙了.只能尝试L2TP了. Ubuntu可视化配置界面network-manager默认是没有L2TP配置选项的,需要安装第三方插件软件: sudo apt-a ...