LCA of a Binary Tree
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的更多相关文章
- 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 ...
- 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 ...
- PAT-1151(LCA in a Binary Tree)+最近公共祖先+二叉树的中序遍历和前序遍历
LCA in a Binary Tree PAT-1151 本题的困难在于如何在中序遍历和前序遍历已知的情况下找出两个结点的最近公共祖先. 可以利用据中序遍历和前序遍历构建树的思路,判断两个结点在根节 ...
- 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 ...
- 【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 ...
- PAT 甲级 1151 LCA in a Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/1038430130011897856 The lowest common anc ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Commons-Collections
package com.bjsxt.others.commons; import java.util.ArrayList; import java.util.List; import org.apac ...
- Types of Entity in Entity Framework:
http://www.entityframeworktutorial.net/Types-of-Entities.aspx We created EDM for existing database i ...
- Restrict each user to a single session in window server 2008 R2 or 2012
Restrict each user to a single session in window server 2008 R2 or 2012 2014-10-31 In window server ...
- hdu 1520 Anniversary party || codevs 1380 树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Map和hash_map
map和hash_map 今天在写拼流的程序时碰到一个问题,要根据流的四元组的结构信息映射到该流的数据.也就是我在网络数据包拼接的过程中,要根据包的地址和端口信息,对应到其对应的一个流的数据上去,把端 ...
- 好,开始没做出来 guess-number-higher-or-lower-ii
https://leetcode.com/mockinterview/session/result/xsicjnm/ https://leetcode.com/problems/guess-numbe ...
- ViewPager的监听事件失效
主要是因为在我项目使用了PageIndicator,所以这个时候监听事件要写在PageIndicator上. mIndicator.setOnPageChangeListener(new OnPage ...
- Servlet生命周期以及获取参数
1. 创建Servlet几种方式 1) 实现Servlet接口 控制Servlet的生命周期 构造器 init() service() des ...
- I.MX6 ov5640 camera
/************************************************************************ * I.MX6 ov5640 camera * 说明 ...
- python练习程序(c100经典例5)
题目: 输入三个整数x,y,z,请把这三个数由小到大输出. def swap(a,b): t=a; a=b; b=t; return (a,b); def foo(x,y,z): if x>y: ...