解题思路一:

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. VS2015新建asp.net core站点

    摘要 电脑上安装了vs2015,今天就尝尝鲜,新建一个项目试试. BBS 使用vs2015新建一个解决方案,然后新建项目,此时你会发现没有asp.net core项目,这就需要先安装asp.net c ...

  2. [Html5]sessionStorage和localStorage的区别

    摘要 有时需要在浏览器中保存一些数据,特别在app中嵌入的h5页面中,需要在webview中保存一些数据,作为客户端的数据持久化. h5中web storage有两种存储方式:sessionStora ...

  3. shell 脚本样例

    1  解压文件,移动文件,删除特定目录 #!/bin/bash pa=$(cd ``; pwd) //获得当前目录的绝对路径 v_dir=${pa} mkdir ${v_dir} dirDist=${ ...

  4. NOIP2010 引水入城 题解

    http://www.rqnoj.cn/problem/601 今天发现最小区间覆盖竟然是贪心,不用DP!于是我又找到这题出来撸了一发. 要找到最上面每个城市分别能覆盖最下面哪些城市,如果最下面有城市 ...

  5. webapp中fixed问题解决方案

    主要问题: 1,头部输入框固定后,只要再滑动内容的话,输入框会随着滑动内容而滑动. 2,在低端机:2.3以下的安卓机,你会发现怎么解决都不行的,系统浏览器是不会支持的,头部底部固定的话会滑动内容而滑动 ...

  6. plt和got

    最近在学习linux高级调试技术.下面就动态库连接这块做了一个实验 首先理解下plt是procedure linkage table,got是global offset table.got表中存放的是 ...

  7. ggplot2 上篇

    title: "ggplot2 上篇" author: "li_volleyball" date: "2016年4月16日" output: ...

  8. php打印出来金字塔

    /*                   空格数($k):   第几($i)层    所以:$k+$i=$m  $k=$m-$i *            *                 3--- ...

  9. 第12天 android studio

    1. http://jingyan.baidu.com/article/215817f7888dc21eda14230d.html Gradle DSL method not found:‘andro ...

  10. ubutu之qq安装

    1.压缩包下载链接 http://pan.baidu.com/s/1nvlgsGh 2.安装教程(引用自百度经验) 如何在Ubuntu中安装QQ