题目

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

思路

这一次说的是一个普通的二叉树,给出两个节点。求他们的最低公共父节点。

回忆一下,当这棵二叉树是二分查找树的时候的解决方式:

二分查找树解法:http://blog.csdn.net/langduhualangdu/article/details/47426339

没错。无论是二分查找树也好还是普通二叉树也好。他们的共同规律就是:所给出的两个节点一定在最低公共父节点的两側

那对于BST来说。能够通过大小进行比較推断是不是在当前节点的两側。普通二叉树怎样比較呢?

事实上,我们能够从反面去考虑这个事情:假设当前节点的某一側子树没有所给节点中的不论什么一个。那是不是就能肯定,该节点一定不是最低父节点(节点重合的情况另说),并且,所求节点一定在还有一側。

因此。我们能够这样:当前节点假设为null,返回null;假设为所给节点当中之中的一个,则返回该节点;否则,求出当前节点左子树的返回值和右子数的返回值,假设左右值都不为空。说明当前节点即为所求节点,否则,返回不为空的那个节点。

相同,当得到所求节点后。还是须要检查所在的树上是不是同一时候存在所给的两个节点。应该比較的是节点的地址而不是值。

代码

public boolean checkExist(TreeNode root, TreeNode p, TreeNode q){
if(root==null)
return false;
boolean pExist = false, qExist = false;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while(!queue.isEmpty()){
TreeNode treeNode = queue.poll();
if(treeNode==p)
pExist = true;
if(treeNode==q)
qExist = true;
if(pExist && qExist)
return true;
if(treeNode.left!=null)
queue.add(treeNode.left);
if(treeNode.right!=null)
queue.add(treeNode.right); }
return false;
}
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
TreeNode candidateNode = search(root, p, q);
if(checkExist(candidateNode,p,q))
return candidateNode;
else {
return null;
}
} public TreeNode search(TreeNode root, TreeNode p, TreeNode q){
if(root==null)
return null;
if(root==p || root==q){
return root;
} else{
TreeNode left = search(root.left, p, q);
TreeNode right = search(root.right, p, q);
if(left!=null && right!=null)
return root;
else {
return left!=null?left:right;
}
}
}

leetcode——Lowest Common Ancestor of a Binary Tree的更多相关文章

  1. [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  2. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  3. Leetcode ——Lowest Common Ancestor of a Binary Tree

    Question Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. ...

  4. 88 Lowest Common Ancestor of a Binary Tree

    原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...

  5. [LeetCode] 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 ...

  6. Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium

    236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...

  7. leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree

    https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...

  8. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  9. 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

随机推荐

  1. Problem C: 矩阵对角线求和

    #include<stdio.h> int main() { ][]; scanf("%d",&n); ,sum2=; ;i<n;i++) ;j<n ...

  2. [转]JSP中常见的Tomcat报错错误解析(二)

    jsp常见错误代码文章分类:Java编程 jsp常见错误代码你用的是weblogic还是tomcat服务器.?出现404和500错误是初学jsp的朋友经常遇到的问题. IIS状态代码的含义 概要 当用 ...

  3. HDU 1507 Uncle Tom's Inherited Land*(二分匹配,输出任意一组解)

    Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  4. NAND Flash大容量存储器K9F1G08U的坏块管理方法

    转: http://www.360doc.com/content/11/0915/10/7715138_148381804.shtml 在进行数据存储的时候,我们需要保证数据的完整性,而NAND Fl ...

  5. futer.get()(如果任务没执行完将等待)

    /** * 获取异步任务的执行结果(如果任务没执行完将等待) */ V get() throws InterruptedException, ExecutionException; Future必要时 ...

  6. 安卓安装ZXING(二维码)SDK

    安卓安装ZXING(二维码)SDK 安装<WIFI共享精灵>后,ZXING(二维码)SDK就有了,扫二维码又快而且精度又高. 真是意外的发现.不需要去下载BARCODE SCANNER.

  7. OpenCV支持向量机SVM对线性不可分数据的处理

    支持向量机对线性不可分数据的处理 目标 本文档尝试解答如下问题: 在训练数据线性不可分时,如何定义此情形下支持向量机的最优化问题. 如何设置 CvSVMParams 中的参数来解决此类问题. 动机 为 ...

  8. [转]SSIS ADO.NET vs OLEDB

    本文转自:http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1a9e3670-9685-4943-913b-123ecf248a9c/ol ...

  9. QT5的程序打包发布(将QT5的工程项目打包成一个exe程序)

    最近,在学习QT5的过程中,想尝试着把自己写的工程程序给打包发布出来,在任何一台windows系统都能运行,这样就不会限于电脑需不需要安装QT安装包了. 首先,先介绍自己使用的环境.我使用的QT版本是 ...

  10. 流畅的python第十六章协程学习记录

    从句法上看,协程与生成器类似,都是定义体中包含 yield 关键字的函数.可是,在协程中,yield 通常出现在表达式的右边(例如,datum = yield),可以产出值,也可以不产出——如果 yi ...