解题思路一:

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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. 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 ...

  5. (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 ...

  6. [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 ...

  7. [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 ...

  8. LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. [leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先

    如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...

随机推荐

  1. highstock 的tooltip框里面的内容 保留两位小数的办法

    $("#flux_chart_container").highcharts('                           },            borderWidt ...

  2. [原] Android快速开发框架-AndroidFine,GitHub开源

    Android快速开发框架 UI组件,不止是简单整合,更易用 沉浸式状态栏,界面更漂亮 左滑返回,非常流畅 简单.可复用.易扩展的底部导航 PagerSlidingTabStrip,导航标签文字颜色和 ...

  3. PHP 使用命名空间(namespace),实现自动加载

    示例: #/DB/MySql.class.php也就是DB文件夹下有MySql.class.php文件 namespace DB; class MySql { public function __co ...

  4. Linux下运行C语言程序

    一.编写C语言的源代码 二.用gcc -c C文件名生成.o文件 三.用gcc -o 可执行文件名 .o文件名 生成可执行文件 四.输入可执行文件名前加./执行可执行文件

  5. FLAG是什么公司

    答: facebook, linkedin,  alphabeta,airbnb,amazon,apple, google

  6. gcc 4.8.3 install centos

    http://blog.csdn.net/xlx921027/article/details/17382643

  7. plsql 建立oracle作业

    --.plsql中学习job --学习job --建表 create table test_job(para_date date); commit; insert into test_job valu ...

  8. [codevs2070]爱情之路

    [codevs2070]爱情之路 试题描述 yh非常想念他的女朋友小y,于是他决定前往小y所在的那块大陆. 小y所在的大陆共有n个城市,m条双向路,每条路连接一个或两个城市.经过一条路ei需要耗费时间 ...

  9. [翻译]opengl扩展教程1

    [翻译]opengl扩展教程1 原文地址https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/extensions.php [翻译]ope ...

  10. UML统一建模编程

    PowerDesigner 可以通过类图直接可视化生成代码 UML模型元素: 表示模型中的某个概念(类.对象.用例.结点.组件.包.接口等等): 表示模型间相互连接的关系(关联.泛化.依赖.聚集).