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. 你猜……你再猜

    『男』:你喜欢我吗? 『女』:你猜. 『男』:喜欢. 『女』:你再猜. 『男』:--

  2. sql语句中能有中文 空格

    EXEC dbo.usp_execute_sql_Prod 'SELECT * FROM dbo.QuanVerify_Log where ticketcode = ''3783665132'' ' ...

  3. cookie使用

    知识拷贝. 理论很简单,而且模式也和大多请求 http://blog.csdn.net/lanmao100/article/details/2328491(源地址).返回状态的SSO差不多.但是有几个 ...

  4. html5 audio音频播放全解析

    序 html5开启了一个新时代,因为它让浏览器本身变得不那么被动,audio api就是一个典型的列子,在html5还没确定之前,如果想要在网页上听音乐看视频唯一的办法就是用flash意思是当你没有给 ...

  5. Simple Factory vs. Factory Method vs. Abstract Factory【简单工厂,工厂方法以及抽象工厂的比较】

    I ran into a question on stackoverflow the other day that sort of shocked me. It was a piece of code ...

  6. 如何获取supersocket的源代码

    源代码的地址:https://github.com/kerryjiang/SuperSocket 安装git之后,可以使用命令行git clone https://github.com/kerryji ...

  7. [POJ1050]To the Max(最大子矩阵,DP)

    题目链接:http://poj.org/problem?id=1050 发现这个题没有写过题解,现在补上吧,思路挺经典的. 思路就是枚举所有的连续的连续的行,比如1 2 3 4 12 23 34 45 ...

  8. Linux同步机制(一) - 线程锁

    1 互斥锁 在线程实际运行过程中,我们经常需要多个线程保持同步. 这时可以用互斥锁来完成任务.互斥锁的使用过程中,主要有 pthread_mutex_init pthread_mutex_destor ...

  9. HDU 3863 (博弈) No Gambling

    这是我见过的最简单的一道博弈了,其实不要被复杂的棋盘吓到了. 首先肯定会有而且仅有一个人胜,而且因为棋盘是对称的,所以先手相对于后手肯定更有优势,那么肯定是先手赢. 这是不是严格的推理,但是确实比较容 ...

  10. 搜索浅谈(Elasticsearch和Lucene4分享)

    刚刚过去的双11,真是给线下运营商好好上了一课.当今的互联网真是炙手可热,大家对互联网的热情是如此之高.相信电商之间的竞争将更加的激烈和残酷,不过,搜索,作为用户体验很重要的一点,各大电商也做的越来越 ...