题目:

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

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

        _______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

代码:

/**
* 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 || root==p || root==q ) return root;
TreeNode* l = Solution::lowestCommonAncestor(root->left, p, q);
TreeNode* r = Solution::lowestCommonAncestor(root->right, p, q);
if ( l && r )
{
return root;
}
else
{
return l ? l : r;
}
}
};

tips:

直接学习大神的思路(http://bookshadow.com/weblog/2015/07/13/leetcode-lowest-common-ancestor-binary-tree/),确实很巧妙。

总体来说,就是分叉找;终止条件是到了叶子或者找到p和q的一个就返回了。

这里有个思维误区,为啥找到p或q的一个就返回了,如果先找到p而q在p的下面呢?那就正好了,反正这俩点如果q在p的下面,那么根据定义p就是p和q的公共祖先。

【Lowest Common Ancestor of a Binary Tree】cpp的更多相关文章

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

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  2. 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

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

  4. Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium

    236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...

  5. 88 Lowest Common Ancestor of a Binary Tree

    原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...

  6. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. [LeetCode] 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. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  9. [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. POJ-2481 Cows---树状数组的运用

    题目链接: https://vjudge.net/problem/POJ-2481 题目大意: if Si <= Sj and Ej <= Ei and Ei - Si > Ej - ...

  2. POJ-2395 Out of Hay---MST最大边

    题目链接: https://vjudge.net/problem/POJ-2395 题目大意: 求MST中的最大边,和POJ-2495类似 思路: 模板直接过 #include<iostream ...

  3. 自己编写shave函数

    import numpy def shave(I,border=None): I = I[border[0]:I.shape[0]-border[0],border[1]:I.shape[1]-bor ...

  4. Python-程序模块化

    一.程序模块化 一个程序可能需要导入自己写的模块,或者需要导入.查找.修改文件等操作.当把程序移植到其他路径执行时,会因为模块或文件路径的变化而报错. 程序模块化,就是将整个程序(包含该程序需要用到的 ...

  5. C#自动更新本地程序

    关于系统的自动更新.近日有一情况是需要将java端后台最新版本的系统文件覆盖本地客户端,简称自动更新了. 本地会获取当前系统的版本号去请求后台java的接口数据.返回给我的是后台压缩包转的base64 ...

  6. 创建Android环境并且安装cordova

    需要eclipse.Andriod SDK.java.Apache ant.Node.js.Genymotion 目录链接: 1.安装adt-eclipse 2.安装JAVA 3.安装Apache a ...

  7. javascript入门笔记3-dom

    1.通过ID获取元素 document.getElementById("id") <!DOCTYPE HTML> <html> <head> & ...

  8. Linux更改ssh端口号,很easy!

    因为公司业务需求,可能涉及到更改ssh远程的端口号,用下面方法轻松解决,废话不多说! 1.打开ssh端口配置文件:vim /etc/ssh/sshd_config,找到如下图所示的端口,改为自己想改的 ...

  9. 关于event loop

    之前写了篇文章 JS运行机制,里面对event loop简单的说明,面试时又遇到了关于该知识点的题目(主要是process.nextTick和setImmediate的执行顺序不太知道,查了之后才知道 ...

  10. 【例题收藏】◇例题·V◇ Gap

    ◇例题·V◇ Gap 搜索训练开始了……POJ的数据比ZOJ强多了!!看来不得不写正解了 +传送门+ ◇ 题目 <简要翻译> 有一个四行九列的矩阵——在第1~4行.2~8列上填上数字 11 ...