Java for LeetCode 236 Lowest Common Ancestor of a Binary Tree
解题思路一:
DFS到每个节点的路径,根据路径算出LCA:
public class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == p || root == q)
return root;
List pList = new ArrayList<TreeNode>(), qList = new ArrayList<TreeNode>();
getPath(root,p,pList);
getPath(root,q,qList);
for(int i=0;i<Math.min(pList.size(), qList.size());i++){
if(pList.get(i)!=qList.get(i))
return (TreeNode) pList.get(i-1);
}
return (TreeNode) pList.get(Math.min(pList.size(), qList.size())-1);
} private boolean getPath(TreeNode root, TreeNode p, List<TreeNode> path) {
path.add(root);
if (root == p)
return true;
if (root.left != null) {
if (getPath(root.left, p, path))
return true;
path.remove(path.size() - 1);
}
if (root.right != null) {
if (getPath(root.right, p, path))
return true;
path.remove(path.size() - 1);
}
return false;
}
}
解题思路二:
后续遍历+并查集(《数据结构》第11章内容),实现的时间空间,复杂度略高
解题思路三:并查集+Tarjan算法
参考:http://baike.baidu.com/link?url=oJBohljiyLYv4B0lFr5nbXOeYQ6B1PD4jAYAAZ0v2jKWR_ygiKyNFYqfIgWYbST0meiByvGTFNEjftX7vst90q#3_1
Java for LeetCode 236 Lowest Common Ancestor of a Binary Tree的更多相关文章
- [LeetCode] 236. 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] 236. 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@ [236] Lowest Common Ancestor of a Binary Tree(Tree)
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...
- leetcode 236. 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 ...
- (medium)LeetCode 236.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]236. Lowest Common Ancestor of a Binary Tree二叉树最近公共祖先
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accordi ...
- [leetcode]236. 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 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- [leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先
如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...
随机推荐
- android自定义控件(1)-点击实现开关按钮切换
自定义控件的步骤.用到的主要方法: 1.首先需要定义一个类,继承自View:对于继承View的类,会需要实现至少一个构造方法:实际上这里一共有三个构造方法: public View (Contex ...
- MEF搜索范围
MEF对扩展组件的查找范围通常有三个: AssemblyCatalog:从某个程序集中查找. ApplicationCatalog:在应用程序所在的目录下查找. DirectoryCatalog:在某 ...
- linux的多媒体 播放 软件版权问题
linux下基本很多 跟多媒体 相关的软件, 都是有版权的, 都是 第三方软件, 都是closed-resource的 都有版权问题, 因此, 几乎所有的 linux的 发行版 都不会带有 多媒体软件 ...
- Java基础相关总结
临近面试,权当复习了吧 final相关 定义常量的方法 eg:final int i=0;//则i不能被修改 final修饰的类不能被继承,因此没有子类,且它的类中的方法默认是final final ...
- 深入理解Javascript面向对象编程
深入理解Javascript面向对象编程 阅读目录 一:理解构造函数原型(prototype)机制 二:理解原型域链的概念 三:理解原型继承机制 四:理解使用类继承(继承的更好的方案) 五:建议使用封 ...
- sqlserver中将某数据库下的所有表字段名称为小写的改为大写
declare @name varchar(50), @newname varchar(50),@colname varchar(50) declare abc cursor for select ( ...
- iOS 修改UIWebView的UserAgent
iOS和H5交互的时候,H5需要用userAgent带一些参数,需要我们修改默认的UserAgent为自定义的. 首先,给大家普及一下userAgent的历史,点击UserAgent查看. 1 在Ap ...
- Net上传附件大小控控值(转)
Server Error 404 – File or directory not found. The resource you are looking for might have been rem ...
- 1_mysql +DBA职业发展
MYSQL + DBA 职业发展 mysql :the world's most popular open source database 最流行的开源数据库 数据库世界 关系数据库(又称SQL数据库 ...
- Javascript软键盘设计
国内大多数网站的密码在网络传输过程中都是明文的,我们目前正在做的产品也是这样的情形,这正常吗? 大家都偷懒?不重视安全?各人持有观点,有人认为明文传输并不是想象中的那么可怕,事实上正常情况下这些报文你 ...