解题思路一:

DFS到每个节点的路径,根据路径算出LCA:

  1. public class Solution {
  2. public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
  3. if (root == p || root == q)
  4. return root;
  5. List pList = new ArrayList<TreeNode>(), qList = new ArrayList<TreeNode>();
  6. getPath(root,p,pList);
  7. getPath(root,q,qList);
  8. for(int i=0;i<Math.min(pList.size(), qList.size());i++){
  9. if(pList.get(i)!=qList.get(i))
  10. return (TreeNode) pList.get(i-1);
  11. }
  12. return (TreeNode) pList.get(Math.min(pList.size(), qList.size())-1);
  13. }
  14.  
  15. private boolean getPath(TreeNode root, TreeNode p, List<TreeNode> path) {
  16. path.add(root);
  17. if (root == p)
  18. return true;
  19. if (root.left != null) {
  20. if (getPath(root.left, p, path))
  21. return true;
  22. path.remove(path.size() - 1);
  23. }
  24. if (root.right != null) {
  25. if (getPath(root.right, p, path))
  26. return true;
  27. path.remove(path.size() - 1);
  28. }
  29. return false;
  30. }
  31. }

解题思路二:

后续遍历+并查集(《数据结构》第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. Cannot install NodeJs: /usr/bin/env: node: No such file or directory

    安装doxmate时,doxmate地址是:https://github.com/JacksonTian/doxmatenpm install doxmate -g 安装完后把错误:Cannot in ...

  2. JS keycode 事件响应

    <script language="javascript"> function keyevent(){ if(event.keyCode==13) alert(&quo ...

  3. 图解Tomcat类加载机制

    说到本篇的tomcat类加载机制,不得不说翻译学习tomcat的初衷. 之前实习的时候学习javaMelody的源码,但是它是一个Maven的项目,与我们自己的web项目整合后无法直接断点调试.后来同 ...

  4. mysql搜索引擎 小结

    mysql搜索引擎 小结 mysql5.5以后,mysql默认使用InnoDB存储引擎. 若要修改默认引擎,可以修改配置文件中的default-storage-engine.可以通过show vari ...

  5. 基于iSCSI的SQL Server 2012群集测试(四)--模拟群集故障转移

    6.模拟群集故障转移 6.1 模拟手动故障转移(1+1) 模拟手动故障转移的目的有以下几点: 测试群集是否能正常故障转移 测试修改端口是否能同步到备节点 测试禁用full-text和Browser服务 ...

  6. Windows Phone 8下 友盟社会化组件SDK的使用。

    由于项目的需要,要将友盟的社会化组件SDK由0.9更新至2.0. 版本变化比较大. 1.很多类以及命名空间已经取消了. 如UmengSocialSDK.Net.Request命名空间, UmengSo ...

  7. Linux 动态监听进程shell

    背景 前几天在研究线程的时候,看到一句话说java里的线程Thread.run都会在Linux中fork一个的轻量级进程,于是就想验证一下(笔者的机器是Linux的).当时用top命令的时候,进程总是 ...

  8. CSS技巧-rgba函数的妙用

    先简单介绍一下: rgba()函数是平时开发中经常遇到的,这篇文章也做了一个比较详细的解读以及一系列的应用. 对它的工作原理做一番分析:就是具有一定透明度的盒子: 还比较了rgba()函数和不透明度属 ...

  9. 当webshell不可执行cmshell时 (菜刀的安全模式!)可用此脚本突破执行cmd命令

    <?php /* ============== */ error_reporting(0); ini_set('max_execution_time',0); // -------------- ...

  10. Win10新版下virtualbox双击没有反应

    百度virtualbox 第一个就是Oracle官网,点击download,点击相应的版本,下载后右键管理员权限去安装,