leetcode236
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == NULL || 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
{
return left == NULL ? right : left;
}
}
};
补充一个DFS实现,使用先序遍历将每一个路径都记录下来,然后分情况讨论。
public class Solution
{
List<List<TreeNode>> V = new List<List<TreeNode>>();
Stack<TreeNode> v = new Stack<TreeNode>(); public void PostTree(TreeNode node)
{
if (node != null)
{
v.Push(node);
if (node.left != null)
{
PostTree(node.left);
v.Pop();
}
if (node.right != null)
{
PostTree(node.right);
v.Pop();
}
if (node.left == null && node.right == null)
{
var list = new List<TreeNode>();
foreach (var s in v)
{
list.Add(s);
}
list.Reverse();
V.Add(list);
} }
} public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
{
PostTree(root);
int p_index = -;
int q_index = -;
for (var i = ; i < V.Count(); i++)
{
if (V[i].Find(x => x.val == p.val) != null)
{
p_index = i;
}
if (V[i].Find(x => x.val == q.val) != null)
{
q_index = i;
}
if (p_index >= && q_index >= )
{
break;
}
}
if (p_index == q_index)
{
var l = V[p_index];
//p和q在同一个路径上
foreach (var va in l)
{
if (va.val == p.val || va.val == q.val)
{
return va;
}
}
}
else
{
//p和q在不通路径上
var l1 = V[p_index];
var l2 = V[q_index];
int i = ;
int j = ;
while (i < l1.Count() && j < l2.Count())
{
if (l1[i].val != l2[j].val)
{
return l1[i - ];
}
i++;
j++;
}
}
return null;
}
}
补充一个python的实现:
class Solution:
#当p和q分别在某节点的左子树和右子树的时候,这个节点就是LCA
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if root == None or root == p or root == q:
return root
left = self.lowestCommonAncestor(root.left,p,q)#在左子树中找p或q
right = self.lowestCommonAncestor(root.right,p,q)#在右子树中找p或q
if left != None and right != None:#p和q分别在这个节点的左右子树,则当前节点就是LCA
return root
elif left != None:#p和q都在这个节点的左子树,则在p和q中,先被找到的就是LCA
return left
elif right != None:#p和q都在这个节点的右子树,则先被找到的就是LCA
return right
else:
return None#p和q不在这个节点的子树中
LCA让我想起了日月生辉的光辉战斗机。
leetcode236的更多相关文章
- [Swift]LeetCode236. 二叉树的最近公共祖先 | 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:二叉树最近公共节点(LCA)《leetcode236》
Lowest Common Ancestor of a Binary Tree(二叉树的最近公共父亲节点) Given a binary tree, find the lowest common an ...
- LeetCode236. 二叉树的最近公共祖先
* @lc app=leetcode.cn id=236 lang=cpp * * [236] 二叉树的最近公共祖先 * * https://leetcode-cn.com/problems/ ...
- 奇安信集团笔试题:二叉树的最近公共祖先(leetcode236),杀死进程(leetcode582)
1. 二叉树最近公共祖先 奇安信集团 2020校招 服务端开发-应用开发方向在线考试 编程题|20分2/2 寻祖问宗 时间限制:C/C++语言 1000MS:其他语言 3000MS 内存限制: ...
- leetcode236 Lowest Common Ancestor of a Binary Tree
""" Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in ...
- leetcode探索高级算法
C++版 数组和字符串 正文 链表: 正文 树与图: 树: leetcode236. 二叉树的最近公共祖先 递归(先序) leetcode124二叉树最大路径和 递归 图: leetcode 547朋 ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
- LeetCode通关:连刷三十九道二叉树,刷疯了!
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- eclipse中创建DataBase Connections
1.window --> show view --> other --> Data Management --> Data Exploerer --> ok: 2.右键单 ...
- 18-11-01 pandas 学习03
[python]pandas display选项 import pandas as pd 1.pd.set_option('expand_frame_repr', False) True就是可以换行显 ...
- 复现 360 Unicorn Team 黑科技之 HackNFC
看了2条360 Unicorn Team的微博后,感觉蛮有趣的,打算复现一下 谷歌了下相关资料,在HACKADAY找到了介绍文章 还有2篇北邮工学硕士的论文,欢迎有兴趣的朋友和我一起交流~ 联系方式在 ...
- Mondrian辅助组件----Schema WorkBench(架构平台简介)
Schema WorkBech 是Pentaho套件的另一个组件,是mondrian中schema文件生成工具.通过Schema WorkBench我们可以快速生成一个schema文件,不再需要手写. ...
- 【转载】 Deepmind星际争霸2平台使用第一轮-完成采矿
原文地址: https://blog.csdn.net/woaipichuli/article/details/78645999 ----------------------------------- ...
- linux shell 指令搜索顺序
在linux shell 中输入一个命令,如果有多个同名指令,shell需要按照一定规则去取优先级高的一个执行,shell命令的搜索顺序为: 1.别名,使用alias创建的命令. 2.关键字,如if, ...
- SQL-视图-004
什么是视图? 优点:视图是保存在数据库中的SELECT查询,可以简化查询操作,视图可以从多个表中提取数据,并以单个表的形式显示结果,这样就可以针对多个表的查询转化为对一个表的查询. 映射:视图可以理解 ...
- Transaction rolled back because it has been marked as rollback-only 原因 和解决方案
产生原因 , 1 serviceA 调用 serviceB 然后 B 抛出异常 ,B 所在的 事物 回滚,B 把当前可写 事物标记成 只读事物 , 2 如果 A 和B 是在 同一个事物环境,并且 ...
- Jenkins 配置 FindBugs,Checkstyle,PMD 实现代码的静态检查 (14)
一.插件介绍 FindBugs:静态分析工具,它检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题.利用这个工具,就可以在不实际运行程序的情况对软件进行分析.它可以帮助改进代码的 ...
- Centos7创建CA和申请证书 转自https://www.cnblogs.com/mingzhang/p/8949541.html
Centos7.3创建CA和申请证书 openssl 的配置文件:/etc/pki/tls/openssl.cnf 重要参数配置路径 dir = /etc/pki/CA ...