[Leetcode 235/236]LCA二叉树最近公共祖先Lowest Common Ancestor of a Binary Tree
题目
给定二叉树和两个点,求两点的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的更多相关文章
- [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 ...
- [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 ...
- 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 利用二 ...
- 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 ...
- 【刷题-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 ...
- 【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 ...
- LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- [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 ...
- [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 ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 自动化处理日志脚本 shell
自动处理脚本 保留7天的,带详细时间戳, #!/bin/sh #description split logs file1=/var/log/messages file2=/var/log/rabbit ...
- Python发送飞书消息
#!/usr/bin/python3.8 # -*- coding:UTF-8 -*- import os, sys sys.path.append(os.path.dirname(os.path.a ...
- kafka监控系统kafka eagle安装和使用
一.kafka eagle介绍 kafka eagle 是一款由国内公司开源的Kafka集群监控系统,可以用来监视kafka集群的broker状态.Topic信息.IO.内存.consumer线程.偏 ...
- mac 编译安装ffmpeg
下载源码: https://ffmpeg.org/download.html 解压, ./configure --disable-x86asm --prefix=/usr/local/ffmpeg_m ...
- Linux firewall 命令
常用命令 开启端口命令 firewall-cmd --zone=public--add-port=443/tcp --permanent --zone #作用域 --add-port=80/tcp ...
- sshpass免密登录源码剖析
源码下载地址:https://sourceforge.net/projects/sshpass/ 免密登陆程序sshpass源码解析,短小精悍的程序,非常值得学习!
- android本地文件处理的一些经验
选择文件后,现在一般返回 Uri contentResolver.getType(selUrl)结果如下 .txt text/plain .jpeg image/jpeg .mp4 video/mp4 ...
- mybatis bind 标签 覆盖 复杂对象的某个属性值 问题。
需求: 有四个sql 都需要用一个 相同的where 条件,于是定义了一个sql 标签. 然后在每个sql中使用 <include refid="myWhereSql"> ...
- vue3.0使用富文本编辑器VueQuill
1. npm install @vueup/vue-quill@alpha --save 2. 在main.js中全局引入 import { QuillEditor } from '@vueup/vu ...
- C#动态创建对象和其属性
dynamic contact = new ExpandoObject(); contact.Name = "Patrick Hines"; contact.Phone = &qu ...