思路:1.如果p或q就是根节点,那么LCA=p或q,返回根节点(递归出口)

2.分治

2.1 Divide:分别计算左字树和右子树的LCA

2.2 Conquer:如果左字树和右子树的计算结果均不为空,则根节点就是p,q的LCA;如果左不为空而右为空,则返回左子树的计算结果;

   如果右不为空而左为空,则返回右子树的计算结果。如果左右子树均为空,则返回空指针

/**
* 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==NULL) return NULL;
if(p==root||q==root) return root;
TreeNode *left=lowestCommonAncestor(root->left,p,q);
TreeNode *right=lowestCommonAncestor(root->right,p,q);
if(left!=NULL&&right!=NULL) return root;
if(left!=NULL) return left;
if(right!=NULL) return right;
return NULL;
}
};

Leetcode 236的更多相关文章

  1. leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)

    https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...

  2. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  3. Leetcode 236.二叉树的最近公共祖先

    二叉树的最近公共祖先 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x ...

  4. leetcode 236. 二叉树的最近公共祖先LCA(后序遍历,回溯)

    LCA(Least Common Ancestors),即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. 题目描述 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百 ...

  5. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  6. Java实现 LeetCode 236 二叉树的最近公共祖先

    236. 二叉树的最近公共祖先 给定一个二叉树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x ...

  7. leetcode 236. Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  8. Java for LeetCode 236 Lowest Common Ancestor of a Binary Tree

    解题思路一: DFS到每个节点的路径,根据路径算出LCA: public class Solution { public TreeNode lowestCommonAncestor(TreeNode ...

  9. (medium)LeetCode 236.Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  10. 【LeetCode 236】Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

随机推荐

  1. 怎么取消环境变量设置 shell

    Linux字符模式下设置/删除环境变量方法: bash下 设置:export 变量名=变量值 删除:unset 变量名 转自:http://zhidao.baidu.com/link?url=sY3u ...

  2. PHP导入导出excel表格图片的代码和方法大全

    基本上导出的文件分为两种: 1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已.修改这种文件后再保存,通常会提示你是否要转换成Excel文件 ...

  3. 一起talk C栗子吧(第十回:C语言实例--最小公倍数)

    各位看官们,大家好.从今天開始,我们讲大型章回体科技小说 :C栗子.也就是C语言实例. 闲话休提, 言归正转.让我们一起talk C栗子吧! 看官们,上一回中咱们说的是最大公约数的样例,这一回咱们说的 ...

  4. $_SERVER,IP,域名常用方法

    PHP $_SERVER详解   $_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 $_SERVER['REMOTE_ADDR'] //当前用户 IP . $_SERVE ...

  5. 在visual studio中运行C++心得

    1.在visual studio中建立C++项目 (1)新建->项目->空项目 C++ (2)右击项目->添加->新建项->C++文件(.app) (3编写C++文件   ...

  6. 第十四篇:Apriori 关联分析算法原理分析与代码实现

    前言 想必大家都听过数据挖掘领域那个经典的故事 - "啤酒与尿布" 的故事. 那么,具体是怎么从海量销售信息中挖掘出啤酒和尿布之间的关系呢? 这就是关联分析所要完成的任务了. 本文 ...

  7. maven web框架搭建

    前面我们描述了如何使用maven搭建一个最基本的Java Project 框架.在实际生产应用中,一般不会仅仅建立一个最基本的Java Project项目,目前 Java Web 开发已经成为Java ...

  8. ajax跨域问题解决之cors篇

    现在浏览器出于安全考虑,在域名.协议.端口不同的情况下,浏览器会认为这是跨域,ajax请求是不允许跨域的. 如果我们有跨域的需求,可以使用cors解决.其原理就是,在请求之前先发送一个OPTIONS请 ...

  9. PAT 甲级 1060 Are They Equal

    1060. Are They Equal (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue If a ma ...

  10. 时间序列模式——ARIMA模型

    ARIMA模型全称为自回归积分滑动平均模型(Autoregressive Integrated Moving Average Model,简记ARIMA),是由博克思(Box)和詹金斯(Jenkins ...