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 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ... 
随机推荐
- windows下配置redis
			1.首先去GitHub上下载所需文件,这里我们下载的是zip文件 https://github.com/MicrosoftArchive/redis/releases 2.解压后文件目录如下 3.启动 ... 
- Linux驱动中常用的宏
			.module_i2c_driver(adxl34x_driver)展开为 static int __int adxl34x_driver_init(void) { return i2c_regist ... 
- statik golang 静态资源嵌入二进制文件工具使用(docker 构建)
			将静态资源打包进二进制文件有好多方便的地方 方便客户演示 代码简单加密 运行方便 statik 就是一款在golang 中用的比较多,nodejs 有一款pkg (oclif 就推荐使用此工具) ... 
- 在Win7下新建库并修改图标
			win7中在库中添加桌面方法详解 1.在空白处,鼠标右键选择新建——库. 2.命名为桌面,然后选择桌面. 3.鼠标右键选择属性. 4.点击包括文件夹. 5.选择桌面,点击包括文件夹按钮. 6.点击确定 ... 
- ComboBox智能搜索功能
			cmbList.AutoCompleteSource = AutoCompleteSource.ListItems; cmbList.AutoCompleteMode = AutoCompleteMo ... 
- 360 杀毒几K每秒的IO读取,SO MAD
			在没有用360杀毒扫描的状态下,从任务管理器中查看,居然有几K每秒的IO读取 . 好卡,直接卸载. 
- 用pyenv 和 virtualenv 搭建单机多版本python 虚拟开发环境
			作为主流开发语言, 用python 开发的程序越来越多. 方便的是大多linux系统里面都默认集成了python, 开发可以随时随地开始. 但有时候这也成为了一个短板, 比如说有时候我们需要开发和调试 ... 
- 基于jQuery.i18n.properties实现前端网站语言多版本
			我是参考播客做了个demo:http://blog.csdn.net/aixiaoyang168/article/details/49336709 jQuery.i18n.properties采用.p ... 
- 峰Spring4学习(2)依赖注入的几种方式
			一.装配一个bean 二.依赖注入的几种方式 com.cy.entity People.java: package com.cy.entity; public class People { pri ... 
- Unity3D 3D模型在GUI之上显示
			原来旧的办法是,在主相机上加一个Panel,把3D模型显示在Panel上面,感觉这个方法不怎么好,现在进行改进: 现在用了两个相机,一个相机显示3D模型,另外一个是主相机.还需要GUITexture来 ... 
