/**
* 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. 字符串哈希算法(以ELFHash详解)

    更多字符串哈希算法请参考:http://blog.csdn.net/AlburtHoffman/article/details/19641123 先来了解一下何为哈希: 哈希表是根据设定的哈希函数H( ...

  2. freemarker在js中的应用

    <script type="text/javascript"> //freemarker在js中的应用: var newOrganizations = []; < ...

  3. hello1与hello2的代码分析

    1.hello1代码分析 hello.java package javaeetutorial.hello1; import javax.enterprise.context.RequestScoped ...

  4. Apache Derby数据库 安装、知识点

    Apache Derby数据库 安装: 下载路径:http://archive.apache.org/dist/db/derby/ 出处:http://www.yiibai.com/hive/hive ...

  5. Windows下 YOLOv3配置教程(YOLOv3项VS2013平台迁移的方法)

    https://blog.csdn.net/maweifei/article/details/81150489

  6. iPhone激活策略说明

    本帖最后由 苏州汇东 于 2014-7-2 19:13 编辑 奉告各位封釉 千万不要泄露机器序列号IMEI号 远程ID真的可以上  只要机器上没有ID 就可以远程上任何ID 我这可以远程上ID 也帮忙 ...

  7. Qt 常用类——QStandardItemModel

    转载:落叶知秋时 类QabstractItemModel,QabstractListModel,QAbstractTableModel不保存数据,用户需要从这些类派生出子类,并在子类中定义某种数据结构 ...

  8. StringIO-将字符串当做文件处理

    StringIO将字符串当做文件处理,十分方便 >>> from StringIO import StringIO >>> file_like_string = S ...

  9. maven学习(1)-简介与安装

    一.Maven 简介 Maven 官网:http://maven.apache.org/ 二.Maven 安装与配置 Maven 下载:http://maven.apache.org/download ...

  10. cookie js案例

    //存cokie function setcookie(keys,value,time){ document.cookie=keys+"="+decodeURIComponent( ...