题目

给定二叉树和两个点,求两点的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. VS Code:4个中文乱码问题及解决方法-转载

    https://www.jianshu.com/p/6a2c21cc07bb   1. 背景   凡是编程软件,特别是国外的软件,都有或多或少的中文乱码问题(毕竟程序都是用英文写的).现提出VS Co ...

  2. 【翻译】了解Flink-数据管道和ETL -- Learn Flink - Data Pipelines & ETL

    目录 无状态转换 map() flatmap() keyed 流 keyBy() keys计算 keyed流上的聚合 (Implicit)状态 reduce() 和其他聚合器 有状态的Transfor ...

  3. Typora笔记收费的解决

    现在的Typora收费了,但是官网还是可以找到历史版本 Typora在1.0版本之前的不收费!!! 所有只需要下载1.0版本之前的版本就可以解决 Windows历史版本地址:https://typor ...

  4. 1144. 递减元素使数组呈锯齿状 (Medium)

    问题描述 1144. 递减元素使数组呈锯齿状 (Medium) 给你一个整数数组 nums,每次 操作 会从中选择一个元素并 将该元素的值减少 1. 如果符合下列情况之一,则数组 A 就是 锯齿数组: ...

  5. hashMap 获取里面value最大的值得key

    public static void main(String[] args) { Map<String, Integer> map = new HashMap(); map.put(&qu ...

  6. oracle 存储过程-动态行转列,解决。

    包头 create or replace package pro_test as TYPE out_cursor IS REF CURSOR; procedure Alarm_ContentsByTi ...

  7. 第四章:基本Git概念(重点)

    本章通过讨论Git的基本架构组成和一些重要概念,来探讨Git的不同之处和原因. 一: 基本概念 1.版本库. 1]Git版本库只是一个简单的数据库,包含所有用来维护与管理项目的修订版本和历史记录. 2 ...

  8. pgsql指定部分字段去重

    -- 基于ig.start_pile,ig.end_pile 字段去重 with ete as ( SELECT * from (SELECT ROW_NUMBER() OVER(PARTITION ...

  9. 杭电oj 蟠桃记

    Problem Description 喜欢西游记的同学肯定都知道悟空偷吃蟠桃的故事,你们一定都觉得这猴子太闹腾了,其实你们是有所不知:悟空是在研究一个数学问题!什么问题?他研究的问题是蟠桃一共有多少 ...

  10. mongodb地理位置坐标加了索引,操作时报错 Location object expected, location array not in correct format

    别犹豫了,将坐标中的数据改为数字类型即可,如: location:[113.45,34,191]