/**
* 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. web 纯 javascript 的MVC 实现的简单实践

    现在javascript是越来越火了,好多javascript框架随之产生,大大简化了我们的开发,一般的开发模式大家是比较喜欢MVC 的model controller view 这种模式 方便了我们 ...

  2. Linux小问题以及解决方案

    1.Linux的时间有问题? ntpdate pool.ntp.org 2.要把一条命令开机执行开机 vim /etc/rc.local 添加要执行的命令 3.系统中网络进程的端口监听情况: nets ...

  3. 先进驾驶员辅助系统ADSA

    ADSA(Advanced Driver-Assistance Systems)字面翻译过来是“先进驾驶员辅助系统”,实际上它是一种“辅助驾驶员更便捷更安全使用汽车”的系统. ADAS的研发历史可以追 ...

  4. Anaconda 使用(解决python包管理与环境管理)

    Anaconda完全入门指南(对python环境和原理,讲的比较透彻):https://www.jianshu.com/p/eaee1fadc1e9 用pip一个一个安装第三方库费时费力,还需要考虑兼 ...

  5. Django Model基础 ORM

    ORM 对象关系映射(英语:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的 ...

  6. Linux操作系统中/sbin/init程序的执行过程

    当init启动后,它通过执行各种启动事务来继续引导进程(检查并监视文件系统,启动后台程序daemons,等等),直至完成用户所有操作环境的设置工作.这里主要涉及4个程序:init.getty(aget ...

  7. TextBox(只允许输入字母或者数字)——重写控件

    实现如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...

  8. ApacheKylin笔记

    基本没有什么难度,只要照着官网上配置就行,这里有以下几点需要注意: HBase一定要用2.0以下的,切记.否则会报找不到方法的错误! 需要配置修改kylin.properties文件里的spark配置 ...

  9. selenium操作隐藏的元素 (下拉框类型)

    有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: Python 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操 ...

  10. 学习笔记之IKM C++ 11

    https://github.com/haotang923/interview/tree/master/IKM Q1. If most of the calls to function foo() b ...