题目

给定二叉树和两个点,求两点的LCA最近公共祖先

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”

Example 1:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

Example 2:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.

Example 3:

Input: root = [1,2], p = 1, q = 2
Output: 1

思路

是一道套路题,A B俩点的位置只有三种情况

- A B 都在root左边(这时LCA必也在左边)

- A B 都在root右边(这时LCA必也在右边)

- A B 分别在root的左右边(这时LCA必为root)

代码

写起来很简单,但没想通为什么回溯之后得到的就是LCA,怎么证明

想通了,必定是LCA。比如俩点都在left,最后fun(root.left,p,q)就是LCA

因为fun(root.left,p,q)再次调用fun

fun(root.left.left,p,q)和fun(root.left.right,p,q)

只有当left和right都不为null时,才返回root(此时的root可能是root.left.left/root.left.right.left等等)

所以,虽然代码中是root==p或root==q,看似只找一个点,其实是俩点都找到back时才返回结果

class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root==p||root==q||root==null)
return root;
TreeNode leftRoot=lowestCommonAncestor(root.left,p,q);
TreeNode rightRoot=lowestCommonAncestor(root.right,p,q); if (leftRoot==null)
return rightRoot;//root的left没有pq,即pq都在右边,LCA也在右边
else if(rightRoot==null)
return leftRoot;//root的right没有pq,即pq都在左边,LCA也在左
else
return root;//俩节点分别在root的左右俩边,那LCA就是rott本身
}
}

[Leetcode 235/236]LCA二叉树最近公共祖先Lowest Common Ancestor of a Binary Tree的更多相关文章

  1. [Swift]LeetCode236. 二叉树的最近公共祖先 | Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  2. [Swift]LeetCode235. 二叉搜索树的最近公共祖先 | Lowest Common Ancestor of a Binary Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  3. leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree

    https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...

  4. Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium

    236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...

  5. 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  6. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree

    Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...

  7. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  8. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最近公共祖先

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  9. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  10. 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 蓝桥杯训练赛二-1467 问题 F: 蓝桥杯基础练习VIP-完美的代价

    题目描述 回文串,是一种特殊的字符串,它从左往右读和从右往左读是一样的.小龙龙认为回文串才是完美的.现在给你一个串,它不一定是回文的,请你计算最少的交换次数使得该串变成一个完美的回文串.交换的定义是: ...

  2. Mac下Virtual Box 6.1 Host-Only 网络配置 没有虚拟网卡

    Virtual Box 6.1 mac下 Virtual Box Host-Only 没有 vboxnet0 点击 tools -> 右边的三杆,点 Network 可以添加,修改,删除  虚拟 ...

  3. JSP课设:学生选课系统(附源码+调试)

    JSP学生选课管理系统学生选课管理系统功能概述(1)登录模块分为两种角色:学生角色.教师角色 (2)教师模块:选课管理功能为对课程信息(课程编号.名称.学分)进行添加.修改.删除操作:学生信息功能对学 ...

  4. taro+vue3+TS+Pinia+tailwindcss+NutUI项目创建与开发

    一.初始化 1.Taro 基本安装并初始化项目 npx @tarojs/cli init WechatAppDemo -选择框架:vue3 -是否使用ts?y -请选择css预编译器:无 -请选择we ...

  5. 记一次对peer的理解

    今天在使用react-native开发项目的时候,想要使用async-storage. 我用的rn版本是18.2.0,不带async-storage,所以我需要自己安装下载. 于是执行命令: npm ...

  6. PPT模板网址

    PPT免费下载网址: https://www.1ppt.com/ PPT模板下载:www.1ppt.com/moban/                行业PPT模板:www.1ppt.com/han ...

  7. [BOM]实现页面loading效果,在图片资源加载完之前显示loading

    使用到jquery. 判断整个页面是否加载完: // 这是根据js判断,页面加载完毕就显示 window.onload = function () { // console.log('load com ...

  8. Mac 下的虚拟机Parallels_Desktop_15

    Mac 下的虚拟机Parallels_Desktop_15 1,取得 Mac Parallels_Desktop_15.dmg 后挂载,密码:7410   2,点关闭!关闭!关闭!,千万不要点&quo ...

  9. 数据驱动DDT(Data-Driven Tests):测试数据的参数化

    准备第三方库: 首先安装ddt库,其次在脚本中引入ddt 打开官网 https://pypi.org/project/ddt/ from ddt import ddt,data,unpack @ddt ...

  10. FFmpeg input与output 函数流程

    重要结构体 AVFormatContext AVCodecContextAVCodecAVPacketAVFrame 0.公共部分 av_register_all(); avfilter_regist ...