题目

给定二叉树和两个点,求两点的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. TP5--页面跳转

    //模拟登录 // $admin = 0; // if($admin != 10){ // //页面跳转 // $this->success('登录失败','login/index'); // ...

  2. homebrew 安装node 切换node版本

    注意:如果之前使用brew install node安装过node,需要先执行brew unlink node来'解绑'node 1.查找可用的node版本 brew search node 2.安装 ...

  3. Centos7 更换yum软件源

    https://blog.csdn.net/zhinian1204/article/details/123975403

  4. oracle 高级队列

    转载:http://www.idevelopment.info/data/Oracle/DBA_tips/Advanced_Queuing/AQ_2.shtml Overview This artic ...

  5. Supervisor安装及配置

    Supervisor安装 # 安装 easy_install supervisor # 生成默认配置文件 echo_supervisord_conf > /etc/supervisord.con ...

  6. rn项目下载@ant-design/react-native时发生冲突

    rn项目,使用npm i @ant-design/react-native下载antd. 下载依赖时报错: 如果你也遇到这个问题,直接告诉你结论,那就是最新的@ant-design/react-nat ...

  7. 城壁 (Rampart)

    题意简述 给定一张 $H \times W $ 的网格图,其中有 \(P\) 个被标记的点,求边长为 \(L\) 或以上的正方形的个数,要求正方形的边不得经过被标记的点. \(1 \le H,W \l ...

  8. [AGC043B] 123 Triangle

    个人思路: 首先,经过 \(1\) 轮就没有 \(3\) 了. 先考虑能否递推前 \(i\) 个数的答案,发现不行. 再考虑能否推出 \(i\) 个数的答案的计算公式,也发现不行. 然后就不会了. 正 ...

  9. ubuntu的vsftpd

    先输入vsftp -v查看自己的ubuntu是不是已经安装了vsftpd 没有安装的样子 安装的样子 如果没有安装先安装,输入下面的东西 apt-get install vsftpd 进行安装,安装好 ...

  10. Python Telnetlib模块连接网络设备

    # -*- coding: UTF-8 -*- import telnetlib import time import datetime import os import json Username= ...