题目如下:https://leetcode.com/problems/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 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.

由于题目已经给定了数据结构和函数(见下方代码部分),并不能愉快地用指向双亲的指针,然而寻找p和q两个结点的公共祖先似乎又要从下方向上找。于是我们可以考虑用递归:要找的是第一个左右孩子都是p或q祖先的结点;或者本身是p,而且是q的祖先的结点。如果符合这两点,直接返回就好;如果不是任何一个祖先,忽略掉;如果是一个的祖先,可以通过递归将这个情况返回,实现从下向上找。

代码如下:

/**
* 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 (root == p || root == q)
{
return root; //找到了一个结点,返回该结点
} //递归
TreeNode *left = lowestCommonAncestor(root->left, p, q);
TreeNode *right = lowestCommonAncestor(root->right, p, q); if (left && right)
{
return root; //待查找结点祖先分别是左右孩子,该结点为结果
}
else if (left)
{
return left; //只是一个的祖先,将结果“上传”
}
return right;
}
};

没时间了啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊

//以下不是2015年11月23日00:00前写的

//然而为保持风格还算统一,还是想加几篇觉得写得不错的博客

附:

http://arsenal591.blog.163.com/blog/static/253901269201510169448656

http://www.cnblogs.com/ocNflag/p/4967695.html

http://blog.sina.com.cn/s/blog_1495db3970102w4wl.html

http://www.cnblogs.com/fighter-MaZijun/p/4979318.html

http://www.cnblogs.com/lqf-96/p/lowest-common-ancestor-of-a-binary-tree.html

再附://有图似乎比我上面写的一坨还是更直观一些

http://www.cnblogs.com/Jueis-lishuang/p/4984971.html

最后的最后:

本来此处想装得有文采一些,写一些现在只想好一半的东西,然而写不写出来对我个人似乎意义不大。//这周似乎也有些忙

感叹两句就好了:

大神似乎与咸鱼们并不是同一种生物

算了……就这一句也够了……

数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary 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】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之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 ...

  4. 88 Lowest Common Ancestor of a Binary Tree

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

  5. 【刷题-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 ...

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

  7. LeetCode Lowest Common Ancestor of a Binary Tree

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

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

  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. shell命令

    1:操作系统:人--使用-->shell(或应用程序)--呼叫-->kernel(核心)--->硬件2:查看shell的种类:# cat /etc/shells3:而这个登入系统的 ...

  2. 出现( linker command failed with exit code 1)错误总结 (转)

    这种问题,通常出现在添加第三方库文件或者多人开发时. 这种问题一般是找不到文件而导致的链接错误. 我们可以从如下几个方面着手排查. 1.以如下错误为例,如果是多人开发,你同步完成后发现出现如下的错误. ...

  3. python内建函数

    人太懒了,博客就慢慢添加吧,这个话题还有很多要学的,后面实践了再来添加,现在就当是开个头. print(dir(__builtins__))  #获取内建属性.函数列表     print(help( ...

  4. Python实例4

    4.输入某年某月某日,判断这一天是这一年的第几天? 正解: 源码:

  5. Python数学函数

    1.Python数学函数 1.abs(x):取绝对值,内建函数 2.math.ceil(x):向上取整,在math模块中 3.cmp(x,y):如果 x < y ,返回-1:如果 x == y ...

  6. java 反射的应用 以及通过反射 用到的工厂模式

    java反射详解 本篇文章依旧采用小例子来说明,因为我始终觉的,案例驱动是最好的,要不然只看理论的话,看了也不懂,不过建议大家在看完文章之后,在回过头去看看理论,会有更好的理解. 下面开始正文. [案 ...

  7. php获取post参数的几种方式 RPC 规定接收取值方式 $GLOBALS['HTTP_RAW_POST_DATA'];

    http://www.cnblogs.com/zhepama/p/4022606.html PHP默认识别的数据类型是application/x-www.form-urlencoded标准的数据类型. ...

  8. 由一个RABBITMQ监听器死循环引出的SPRING中BEAN和MAPPER接口的注入问题

    1 @Slf4j 2 @RestController 3 @Component 4 public class VouchersReceiverController implements Message ...

  9. 2015年我国IT行业发展趋势分析(转)

    中国信息化建设步伐正持续推进,行业和企业信息化应用水平也在不断提高,这使得it技术与应用系统已成为企业日常工作中不可或缺的基础设施.与此同时,用户关注的重心也日益转向it系统的稳定性.系统对生产效率的 ...

  10. notepad++

    plugin-manager 先要保证plugin-manager是最新的,然后才能下载其它的插件进行安装. compare插件,文件对比工具