leetcode235
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution {
Stack<TreeNode> SP = new Stack<TreeNode>();
Stack<TreeNode> SQ = new Stack<TreeNode>(); bool findP = false;//是否找到p
bool findQ = false;//是否找到q List<TreeNode> listP = new List<TreeNode>();
List<TreeNode> listQ = new List<TreeNode>(); private void DFS(TreeNode root, int p, int q)
{
if (root != null)
{
if (findP && findQ)
{
return;
} if (!findP)
{
SP.Push(root);
}
if (!findQ)
{
SQ.Push(root);
} if (root.val == p)
{
//p结点寻找结束
findP = true;
listP = SP.ToList();
}
if (root.val == q)
{
//q结点寻找结束
findQ = true;
listQ = SQ.ToList();
} if (root.left != null)
{
DFS(root.left, p, q);
}
if (root.right != null)
{
DFS(root.right, p, q);
} if (!findP)
{
SP.Pop();
}
if (!findQ)
{
SQ.Pop();
}
}
} public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)
{
if (root == null || p == null || q == null)
{
return null;
}
//采用深度优先搜索,找到p和q
DFS(root, p.val, q.val); for (int i = ; i < listP.Count; i++)
{
for (int j = ; j < listQ.Count; j++)
{
var nodep = listP[i];
var nodeq = listQ[j];
if (nodep.val == nodeq.val)
{
return nodep;
}
}
}
return null;
}
}
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/#/description
leetcode235的更多相关文章
- [LeetCode235]Lowest Common Ancestor of a Binary Search Tree
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in th ...
- [Swift]LeetCode235. 二叉搜索树的最近公共祖先 | Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- LeetCode235 二叉搜索树的最近公共祖先
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖 ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
- LeetCode通关:连刷三十九道二叉树,刷疯了!
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...
随机推荐
- 虚拟机CentOS的NAT模式联网和SecureCRT远程登录管理工具
Cenos7 发生了很大的变化,不过也是直接配置网络,从启网卡,从启机器,crt链接 https://blog.csdn.net/gebitan505/article/details/54584213 ...
- 使用slot编写弹窗组件
具体slot用法详见http://www.cnblogs.com/keepfool/p/5637834.html html: <!--测试弹窗--> <dialog-test v-i ...
- PDFSharp生成PDF (转)
http://www.cnblogs.com/zhouxin/p/3228108.html 在上面用OpenXML生成word后,原来利用Word2010里的导出成PDF功能就不能用. 然后找开源组件 ...
- StreamSets 管理 SDC Edge上的pipeline
可选的方式: ui (data colelctor) 发送命令 UI 主要是创建edge pipeline 的时候进行edge server 的配置 默认是 http://localhost:1863 ...
- 看懂Class文件的装载流程
Class文件的加载过程 ClassLoader的工作模式 类的热加载 1 Class文件的装载流程 只有被java虚拟机装载的Class类型才能在程序中使用(注意装载和加载的区别) 1.1 类装载的 ...
- 如何安装Genymotion模拟器
我们在进行App测试的时候,除了使用真机进行测试,有时候还需要借助模拟器来进行测试,那么Android SDK本身给我们提供了一个原生态的模拟器,但是由于启动太慢,性能太差,逐渐被大家放弃了,那么还有 ...
- axis2开发webservice总结
需求环境:对接方公司提供wsdl文件,我方按照该wsdl文件开发服务端. 配置axis2开发环境,网上教程很多,不再啰嗦.环境搭好后执行wsdl2java -uri file:///C:/Users/ ...
- 把存储过程SELECT INTO到临时表
在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种. 一. SELECT INTO1. 使用select into会自动生成临时表,不需要事先创建12 select * into #te ...
- [转]MSSQL 判断临时表是否存在
原文来自:http://www.cnblogs.com/szfhquan/p/4229150.html 方法一: 1 if exists (select * from tempdb.dbo.sysob ...
- [UE4]使机器人受伤