/**
* 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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. LeetCode235 二叉搜索树的最近公共祖先

    给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖 ...

  4. leetcode_二叉树篇_python

    主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...

  5. LeetCode通关:连刷三十九道二叉树,刷疯了!

    分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们 ...

随机推荐

  1. day3 自动部署安装软件到其他的机器设备上

    PS:原理是在本机创建boot.sh指向每一台主机,使用脚本命令去执行,然后就会自动安装软件 PS:boot.sh里面放着1.免密登录 2.发送每台机器install.sh 这个install.sh中 ...

  2. ory Oathkeeper cloud native 访问认证平台

    ORY Oathkeeper is an Identity & Access Proxy (IAP) that authorizes HTTP requests based on sets o ...

  3. php empty详解

    判断字符串是否为空,可以这么判断: if ($value=="") ...    * 格式:bool empty ( mixed var )    * 功能:检查一个变量是否为空  ...

  4. 01.ubuntu14.04安装HI3518EV200 SDK的过程

    转载,侵删 1.海思SDK安装编译 Hi3518EV200_SDK是基于Hi3518EV200_DMEB的软件开发包,包含了在Linux相关应用开发时使用的各种工具及其源代码,是用户开发中最基本的软件 ...

  5. echarts 知识点

    echarts map 禁止放大缩小,设置 calculable 为 false 即可. calculable: false echarts 报错: There is a chart instance ...

  6. mysql的一些 参数查询

    1 查询 事务 超时时间: SHOW GLOBAL VARIABLES LIKE 'innodb_lock_wait_timeout'; (默认innodb引擎事务的超时时间) 2 查询事务隔离级别 ...

  7. vs2010开发环境恢复--(mysql,数据文件可直接拷贝,并可用navicat直接管理)

    一.linq to mysql (DBLINQ) 1.安装mysql phpstudy2014,数据库文件可直接拷贝,在命令行中运行select version();查看版本为5.5.38 ,单独安装 ...

  8. Linux中的其他命令

    1.  修改文件的所有者 chown 用户名  文件名 2. 修改文件所属组 chgrp 组名  文件名 3. 创建用户,创建组,将用户添加到组中等 修改film文件夹的所有者和所属组 修改film文 ...

  9. Debug---Eclipse断点调试基础

    1.进入debug模式(基础知识列表)1.设置断点 2.启动servers端的debug模式 3.运行程序,在后台遇到断点时,进入debug调试状态 ========================= ...

  10. Win10 UEFI +Ghost +intel快速启动 新法完美安装

    http://tieba.baidu.com/p/4767004289?qq-pf-to=pcqq.c2c Win10 如果主板不用Bios 而用UEFI引导启动,速度快,界面好.现在一般都用win1 ...