236. Lowest Common Ancestor of a Binary Tree

     /**
* 基础版
* 给定p,q都是在树中
* 有两种情况:
* 1. p和q分布在LCA的两侧
* 2. p和q在同一侧(p是q的祖先 或者是 q是p的祖先)
* 先考虑第一种情况,例如:
* 1
* / \
* 2 4
* 2和4的LCA是1,向下递归返回时,2返回2,4返回4,1接受到的left和right的返回值都存在值,那么就返回自己
* 也就是说=> 只要找到目标节点,就往上返回该节点
* ---------------------------------
* 第二种情况,例如:
* 1
* /
* 2
* \
* 3
* 2和3的LCA是2,2接收到的是3,而自己也是目标节点,那么就返回自己
* --------------注意1----------------
* 不用考虑在1处,只有一个返回值,如何判断是否有效。因为:两个节点必定在树中,如果root接收到一个值,那么就表示root之下就有p和q的LCA
* 如果接收到两个值,那么自己就是LCA
* --------------注意2----------------
* Corner case: p就是root 或者 q就是root
*/
public static TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
// base case:
// 如果当前结点(root)是p或者是q,就返回自己
if (root == null || root == p || root == q) {
return root;
} TreeNode leftNode = lowestCommonAncestor(root.left, p, q);
TreeNode rightNode = lowestCommonAncestor(root.right, p, q); if (leftNode != null && rightNode != null) { // 接收到两个值,返回root
return root;
} else if (leftNode != null && rightNode == null) { // 接收到一个值,就返回该node
return leftNode;
} else {
return rightNode;
}
}

LCA of a Binary Tree的更多相关文章

  1. PAT 1151 LCA in a Binary Tree[难][二叉树]

    1151 LCA in a Binary Tree (30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

  2. PAT_A1151#LCA in a Binary Tree

    Source: PAT A1151 LCA in a Binary Tree (30 分) Description: The lowest common ancestor (LCA) of two n ...

  3. PAT-1151(LCA in a Binary Tree)+最近公共祖先+二叉树的中序遍历和前序遍历

    LCA in a Binary Tree PAT-1151 本题的困难在于如何在中序遍历和前序遍历已知的情况下找出两个结点的最近公共祖先. 可以利用据中序遍历和前序遍历构建树的思路,判断两个结点在根节 ...

  4. PAT A1151 LCA in a Binary Tree (30 分)——二叉树,最小公共祖先(lca)

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  5. 【PAT 甲级】1151 LCA in a Binary Tree (30 分)

    题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has bo ...

  6. PAT 甲级 1151 LCA in a Binary Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/1038430130011897856 The lowest common anc ...

  7. 1151 LCA in a Binary Tree(30 分)

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  8. PAT Advanced 1151 LCA in a Binary Tree (30) [树的遍历,LCA算法]

    题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...

  9. 1151 LCA in a Binary Tree

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

随机推荐

  1. [JWFD开源工作流]JWFD开源工作流官方下载内容更新

    在更新版的JWFD二次开发包中,我正在实现单线程的时钟控制器,动了下引擎的源代码,这个更新包主要是升级界面,内核代码,大家就不用升级了.. 代码提示: 请修改代码包中(org.jwfd.workflo ...

  2. apache 配置用户级目录

    如果你只需要在用户目录下使用apache的话,还有一个最简单的方式,直接将 httpd.conf文件下的 DocumentRoot "/Library/WebServer/Documents ...

  3. Qt之QHostInfo

    简述 QHostInfo 类为主机名查找提供了静态函数. QHostInfo 利用操作系统提供的查询机制来查询与特定主机名相关联的主机的 IP 地址,或者与一个IP地址相关联的主机名.这个类提供了两个 ...

  4. 深入.NET平台和C#编程 错题录

    1.在C#中,关于文件操作相关的类说法正确的是(AB) <选择二项> A:FileInfo类提供了用于操作文件的实例方法 B:File类提供了用于操作文件的静态方法 C:Directory ...

  5. 51nod1188 最大公约数之和 V2

    考虑每一个数对于答案的贡献.复杂度是O(nlogn)的.因为1/1+1/2+1/3+1/4......是logn级别的 //gcd(i,j)=2=>gcd(i/2,j/2)=1=>phi( ...

  6. 2016年4月TIOBE编程语言排行榜 Visual Basic正渐行渐远

    COBOL, BASIC 和 FORTRAN 很长一段时间作为主力开发语言被使用.有很多软件使用这些语言来编写,并且发展的不亦乐乎.然而经过多年的发展,COBOL和FORTRAN逐渐被抛弃, 而得益于 ...

  7. [xUnix 开发环境--01] MAMP mac os 10.10 配置经历、要点——01. phpmyadmin连不上

    Mac OS 10.10已经自带了apache2和php(php的路径我至今还没不知道,太懒没去找) 用brew安装mysql, 在官网上下载了phpmyadmin,按官方方式配置完后,登录不上,也不 ...

  8. dos保存adb logcat读取的Android信息

    /***************************************************************************** * dos保存adb logcat读取的A ...

  9. mysql无法插入中文字符解决

    1. 基于可维护的角度,虽然latin1没什么问题,但是还是尽量换成utf8或者gb系列 2. 出现乱码时: SHOW VARIABLES LIKE 'character%'SHOW VARIABLE ...

  10. SAS9.4 安装注意事项

    OS:Windows Server 2012 R2 SAS:9.4 TS1M1 SAS9.4的安装需要注意的地方: 一. 注意开启Shortname(装了N遍,最容易忘记的地方) SAS安装使用Sho ...