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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- golang结构体、接口、反射
struct结构体 struct用来自定义复杂数据结构,可以包含多个字段属性,可以嵌套; go中的struct类型理解为类,可以定义方法,和函数定义有些许区别; struct类型是值类型. struc ...
- vue导出excel
1.按装依赖 cnpm install -S file-saver xlsx cnpm install -D script-loader 2.引入Blob.js和expor2Excal.js 3.在m ...
- Postman插件使用
Postman插件:模拟前端请求后台,用于后台对外接口测试 POSTMAN解决..先解压Postman_v4.1.3.rar 然后按自己实际解压的文件夹里面的这个文件夹_metadata前面的_去掉 ...
- LINUX系统配置
LINUX系统配置 Linux 安装jdk方法; Linux Tomcat 安装与配置 Linux redis 安装与配置 (例1) Linux redis安装配置(例2) NGINX 安装 Linu ...
- Dubbo的全局Filter配置
前言: 之前也写过dubbo的filter的文章, 后来和同事也有过交流, 才发生自己对dubbo的filter的机制, 还是存在一些误解, 尤其是自定义filter的定位, 不是那么清晰. 本文主要 ...
- 【leetcode】453. Minimum Moves to Equal Array Elements
problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...
- [ACM International Collegiate Programming Contest, Amman Collegiate Programming Contest (2018)]
https://codeforces.com/gym/101810 A. Careful Thief time limit per test 2.5 s memory limit per test 2 ...
- 细谈getRequestDispatcher()与sendRedirect()的区别
问题?细谈getRequestDispatcher()与sendRedirect()的区别 首先我们要知道: (1)request.getRequestDispatcher()是请求转发,前后页面共享 ...
- YAML 知识点
YAML:Ain't Markup Language 的缩写 YAML文件定义了一系列带有约束说明的任务,这些任务都是以任务名开始并且至少要包含script部分. 任务是由Runners接管并且由服务 ...
- laravel的filter()方法的使用 (方法使用给定的回调函数过滤集合的内容,只留下那些通过给定真实测试的内容)
filter 方法使用给定的回调函数过滤集合的内容,只留下那些通过给定真实测试的内容: $collection = collect([1, 2, 3, 4]); $filtered = $collec ...