题目

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. Java并发(六):volatile的实现原理

    synchronized是一个重量级的锁,volatile通常被比喻成轻量级的synchronized volatile是一个变量修饰符,只能用来修饰变量. volatile写:当写一个volatil ...

  2. Problem E: 调用函数,整数逆序输出

    #include<stdio.h> int reverse(int number)//定义函数 { ;//result用于储存结果 ) { result=result*; i=number ...

  3. Educational Codeforces Round 8 E. Zbazi in Zeydabad 树状数组

    E. Zbazi in Zeydabad 题目连接: http://www.codeforces.com/contest/628/problem/D Description A tourist wan ...

  4. 基于tiny4412的Linux内核移植 -- eMMC驱动移植(六)

    作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...

  5. Android内存优化1 了解java内存分配 1

    开篇废话 今天我们一起来学习JVM的内存分配,主要目的是为我们Android内存优化打下基础. 一直在想以什么样的方式来呈现这个知识点才能让我们易于理解,最终决定使用方法为:图解+源代码分析. 欢迎访 ...

  6. [UML] UML中类之间的几种关系

    类之间可能存在以下几种关系:关联(association).依赖(dependency).聚合(Aggregation,也有的称聚集).组合(Composition).泛化(generalizatio ...

  7. 永远不要去B网(Bittrex.com)

    永远不要去Bittrex.com,没见过这么垃圾的服务! 注册之后基本资料就不能修改了,结果不能提现,充值却是可以充值,就跟今年初禁比特币时的垃圾火币网一样,只进不出,去他奶奶的! 随后网站提示可以高 ...

  8. war后缀的文件

    其实war文件就是Java中web应用程序的打包.借用一个老兄的话,"当你一个web应用程序很多的时候,如果你想把它部署到别的机器上,来回拷这些文件是件挺郁闷的事情,如果要是一个文件就好了. ...

  9. dl,dt,dd标记在网页中要充分利用

    dl,dt,dd标记在网页中要充分利用 来源:网络整理 时间:08-05-27 点击: 点击这里收藏本文 我们在制作网页过程中用到列表时一般会使用<ul>或者<ol>标签,很少 ...

  10. scrapy 抓取数据被禁止的解决方法

    在用抓取头条新闻的数据时出现以下问题:禁止抓取,结果数据没有出来 后来经过查询得知需要把settings.py里面 修改为ROBOTSTXT_OBEY = False就可以了, 默认True