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.
思路
这一次说的是一个普通的二叉树,给出两个节点。求他们的最低公共父节点。
回忆一下,当这棵二叉树是二分查找树的时候的解决方式:
二分查找树解法: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的更多相关文章
- [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 ...
- LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- 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. ...
- 88 Lowest Common Ancestor of a Binary Tree
原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...
- [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 ...
- 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 ...
- 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 利用二 ...
- 【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 ...
- 【刷题-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 ...
随机推荐
- 8VC Venture Cup 2016 - Final Round D. Preorder Test 二分 树形dp
Preorder Test 题目连接: http://www.codeforces.com/contest/627/problem/D Description For his computer sci ...
- CentOS 6.9下双网卡绑定单个IP地址及装网卡绑定到一个网桥(转)
说明:经过查阅,原来双网卡绑定一个IP的专业名词叫做bond,可以实现负载均衡:如果想要实现两张网卡绑定到一个网桥,基本思路是两张网卡设置同一个网桥是行不通的,但如果先实现bond,然后将bond绑定 ...
- ECMAScript 6(ES6)常用语法
一:Let和const (1)Let定义块级作用域的变量,var定义的变量会提升.Let不会提升. 如下.var可以先用,打印是undefined但是let在定义之前是不能用的. 会报错Uncaug ...
- OCP升级(3.6->3.7)
有个好文档还是靠普很多,感谢同事的文档.升级步骤记录如下 1.检查现有环境 [root@master ~]# etcd --version etcd Version: Git SHA: 1674e68 ...
- 关于Java代码优化的44条建议!
关于Java代码优化的N条建议! 本文是作者:五月的仓颉 结合自己的工作和平时学习的体验重新谈一下为什么要进行代码优化.在修改之前,作者的说法是这样的: 就像鲸鱼吃虾米一样,也许吃一个两个虾米对于鲸鱼 ...
- xss payload
xss payload可以使用富客户端文本书写,大多数用javascript,少部分用actionscript等等. 1.盗取cookie,发起cookie劫持 使用xss漏洞插入cookie.js ...
- NTP for Linux
一.服务概述 NTP(network time protocol 网络时间协议)服务器是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源做同步化,提供高精度的时间校正,在LAN上与 ...
- (转)如何在maven环境中设置JVM参数
有时候我们需要设定maven环境下的JVM参数,以便通过maven执行的命令或启动的系统能得到它们需要的参数设定.比如:当我们使用jetty:run启动jetty服务器时,在进行热部署时会经常发生:J ...
- JVM类加载机制详解(一)JVM类加载过程
http://blog.csdn.net/zhangliangzi/article/details/51319033 http://chenzhou123520.iteye.com/blog/1597 ...
- Junit核心——测试类(TestCase)、测试集(TestSuite)、测试运行器(TestRunner)
首先,把这三个定义简单的说明一下: 1.测试类(TestCase):一个包含一个或是多个测试的类,在Junit中就是指的是包含那些带有@Test注解的方法的类,同一样也被称作“测试用例”; 2.测试集 ...