数据结构与算法(1)支线任务4——Lowest Common Ancestor of a Binary Tree
题目如下: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的更多相关文章
- 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 利用二 ...
- 【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 ...
- 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 ...
- 88 Lowest Common Ancestor of a Binary Tree
原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...
- 【刷题-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 ...
- [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 ...
- LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- [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 ...
- [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 ...
随机推荐
- 上下文管理、线程池、redis订阅和发布
一:上下文管理: 对于一些对象在使用之后,需要关闭操作的.比如说:socket.mysql数据库连接.文件句柄等. 都可以用上下文来管理. 语法结构: Typical usage: @contextm ...
- android 5.0以下版本使用atof报错解决
经过测试,如果手机系统在5.0之下,项目project.properties的target若在5.0以上(android-20), NDK 使用atof就会报错: cannot locate symb ...
- (实用篇)浅谈PHP拦截器之__set()与__get()的理解与使用方法
"一般来说,总是把类的属性定义为private,这更符合现实的逻辑. 但是,对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数"__get()"和&q ...
- Java 第十周学习总结
20145113<Java程序设计>第十周学习总结 基础知识 1.网络通讯的方式主要有两种 TCP(传输控制协议)方式:需要建立专用的虚拟连接以及确认传输是否正确 UDP(用户数据报协议) ...
- angular2 - content projection-
angular2中的内容映射: App.component: <my-day> <my-lucky> </my-lucky> </my-day> MyD ...
- 9本java程序员必读的书(附下载地址)
本文列出的9本书在Java程序员界都是被认为很棒的书.当一个程序员开始初学Java时,他的第一个问题应该是如何选择一本书来作为指导学习Java.这个问题也就表明,相对于其他的教程和博客,Java书籍还 ...
- linux apache 配置URL地址栏大小写不敏感配置
1.apache配置 解决如下:把mod_speling.so放到apache目录下的 lib中... 然后修改http.conf文件, 加入:LoadModule speling_module /u ...
- Robot Test Framework + Selenium 的几个坑
现有的webtest是基于Robot 和 Selenium 来写的,没出问题的时候还挺好的,出了问题想debug介个麻烦啊(也可能是姿势不对), 特罗列如下,如有不对,求指正,指导. 1. RIDE ...
- (转) Deep Learning Research Review Week 2: Reinforcement Learning
Deep Learning Research Review Week 2: Reinforcement Learning 转载自: https://adeshpande3.github.io/ad ...
- Python之路,day8-Python基础
***面向对象的好处*** 更容易扩展.提高代码使用效率,使你的代码组织性更强,更清晰 更适合复杂项目的开发 封装 把功能的实现细节封装起来,只暴露调用接口 继承 多态 接口继承 定义 类----&g ...