题目

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. [Luogu1462]通往奥格瑞玛的道路

    题目大意: 一个n个点,m条边的图,每个边有一个边权,每个点也有一个点权. 现在要找一条从1到n的路径,保证边权和不超过b的情况下,最大点权尽量小. 问最大点权最小能是多少? 思路: 二分答案,然后D ...

  2. 使用create-react-app时的something is already running on port 3000

    问题: 自己今天在使用create-react-app搭建react应用的时候,开启了两个React app,然后npm start的时候,出现something is already running ...

  3. .net设置中GridView自适应列宽

    有一个项目只有30分钟开发时间,速成,使用了古老的.net gridview. 但需要列宽自适应好看些. 于是琢磨了,实现思路如下. 先看下大致效果(很粗暴没有优化) 代码如下: protected ...

  4. JavaScript 开发的45个技巧

    JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发(Node.js和Wakanda)等等.JavaScript还是 ...

  5. ubuntu系统使用dnw下载程序

    转:http://blog.chinaunix.net/uid-22030783-id-3350840.html 获得dnw4linux.tar.bz2 源码包,可在xcembed论坛下载:http: ...

  6. Java并发包之闭锁/栅栏/信号量(转)

    本文转自http://blog.csdn.net/u010942020/article/details/79352560 感谢作者 一.Java多线程总结: 描述线程的类:Runable和Thread ...

  7. arcgis andriod 点击后变色

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  8. pyqt5生成的APP制作DMG

    http://blog.csdn.net/wudj810818/article/details/70557284 1.环境 Mac OS Python3.5 PyQt5 2.所需工具 Pyinstal ...

  9. from表单实现无跳转上传文件,接收页面后台数据

    实现无跳转发送表单数据.文件,并能接收后台返回的数据. 主要技术要点: 1.form表单添加target属性,指定一个iframme的name:form表单提交后在iframe内嵌窗口接受响应,主页面 ...

  10. DevExpress控件使用小结

    摘自: http://blog.sina.com.cn/s/blog_95cfa64601019wex.html .TextEditor(barEditItem)取文本 string editValu ...