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. linux系统的权限介绍

    让我们用t o u c h命令创建一个文件:$ touch myfile现在对该目录使用ls -l命令: 我们已经创建了一个空文件,正如我们所希望的那样,第一个横杠告诉我们该文件是一个普通文件.你将会 ...

  2. 在windows系统上安装VMware Workstation虚拟机,然后在虚拟机VMware Workstation上安装linux系统,在linux系统安装xshell的服务端,在windows系统上安装xshell。用windows系统上的xshell连接到linux

    第一步:安装xshell: 去百度   xshell ,然后安装一下就可以了.就是普通的软件安装,在这里不做过多的接收. 第二步:安装虚拟机VMware Workstation 百度安装,不做过介绍 ...

  3. YTU 2607: A代码填空题--更换火车头

    2607: A代码填空题--更换火车头 时间限制: 1 Sec  内存限制: 128 MB 提交: 91  解决: 73 题目描述 注:本题只需要提交填写部分的代码,请按照C++方式提交. 假设火车有 ...

  4. 《OD学hadoop》Linux基础

    一.Linux基本环境 1. Linux常见版本及VMware虚拟机安装Linux系统 2. 虚拟机网络配置(IP地址.主机名.防火墙) 3. 文件基本命令操作 4. 四大远程连接工具使用 二.Lin ...

  5. binary-tree-maximum-path-sum(mock)

    注意: // 注意,如果一个类放在另一个类里面,初始化时候会报错 Solution is not a enclosing class// 这是因为如果TreeNode不是static,那么要求先有外部 ...

  6. Spring安全框架 Spring Security

    Spring Security 的前身是 Acegi Security ,是 Spring 项目组中用来提供安全认证服务的框架. Spring Security  为基于J2EE企业应用软件提供了全面 ...

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

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

  8. zoo.cfg配置

    zookeeper的默认配置文件为zookeeper/conf/zoo_sample.cfg,需要将其修改为zoo.cfg.其中各配置项的含义,解释如下: 1.tickTime:CS通信心跳时间 Zo ...

  9. codevs 1138 聪明的质监员

    二分+前缀和. #include<iostream> #include<cstdio> #include<cstring> #include<cmath> ...

  10. struts2自定义声明校验器

    1 //新建一个validators.xml在src根资源下,会覆盖default.xml的validators,所以你懂得 //接着,若使用声明式校验,则要把配置文件xxxAction-valida ...