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. java 的UUID的具体用法

    参照JDK public final class UUIDextends Objectimplements Serializable, Comparable<UUID> 表示通用唯一标识符 ...

  2. 【Todo】InnoDB、MyISAM、数据库引擎

    关于InnoDB和MyISAM引擎的对比,下面这篇讲的挺好 http://www.cnblogs.com/vicenteforever/articles/1613119.html 这一篇关于InnoD ...

  3. Android - View绘图原理总结

      Android系统的视图结构的设计也采用了组合模式,即View作为所有图形的基类,Viewgroup对View继承扩展为视图容器类,由此就得到了视图部分的基本结构--树形结构 View定义了绘图的 ...

  4. POI刷新数据后的函数(公式)更新问题

    使用POI将Excel模板中的数据进行更新,这应该是很常见的操作 下面就贴上我的一小段代码 public class ModifyExcel { /** * @param fileName Excel ...

  5. HDU 1072 (不一样的入队条件) Nightmare

    之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次. 那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间). 为什么呢?我们 ...

  6. hdu4605 magic ball game 树状数组+离线处理

    题意:给你一棵二叉树,每个节点有一个w值,现在有一颗小球,值为x,从根节点往下掉,如果w==x,那么它就会停止:如果w>x,那么它往左.右儿子的概率都是1.2:如果w<x,那么它往左儿子的 ...

  7. 配置openerp的开发环境

    给Eclipse安装PyDev插件启用Eclipse————如果前面的步骤都正确无误的话,那么Eclipse就该能够正常启动了.第一次启动会让你选择一个工作空间,按缺省设置,勾选一下不再提醒,就可以了 ...

  8. 涵盖网站基本使用的正则表达式的验证方法.cs

    using System; using System.Text.RegularExpressions; using System.Text; using System.IO; using System ...

  9. 二、CSS 基本介绍

    [ 显示目录 ] [ 隐藏 ] 目录 基本概念 CSS组成部分 CSS的规则 引入CSS样式的方法 颜色的表示 CSS Reset 选择器分类 浮动 盒子模型 box-sizing属性 实例:实现“田 ...

  10. BOM浏览器对象

    BOM 浏览器对象 一.浏览器本身就自己有一些对象,不用创建就可以使用 window(当前浏览器窗体) 属性: status opener closed parent top 方法: alert(); ...