Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)
Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)
深度优先搜索的解题详细介绍,点击
给你一个有根节点的二叉树,找到它最深的叶节点的最近公共祖先。
回想一下:
- 叶节点 是二叉树中没有子节点的节点
- 树的根节点的 深度 为
0,如果某一节点的深度为d,那它的子节点的深度就是d+1 - 如果我们假定
A是一组节点S的 最近公共祖先,<font color="#c7254e" face="Menlo, Monaco, Consolas, Courier New, monospace">S</font> 中的每个节点都在以A为根节点的子树中,且A的深度达到此条件下可能的最大值。
示例 1:
输入:root = [1,2,3]
输出:[1,2,3]
示例 2:
输入:root = [1,2,3,4]
输出:[4]
示例 3:
输入:root = [1,2,3,4,5]
输出:[2,4,5]
提示:
- 给你的树中将有 1 到 1000 个节点。
- 树中每个节点的值都在 1 到 1000 之间。
分析:
根据题目,需要先找到最深的叶子节点,然后求最深的叶子节点的最近公共祖先。
那么我们可以遍历这个树,发现左节点的最大高度=右节点的最大高度时,就可以直接返回这个树,如示例1.
如果高度不等,则再进入高度大的那一边继续遍历,找到左高度=右高度为止。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode lcaDeepestLeaves(TreeNode root) {
if(root==null){
return null;
}
int left = depth(root.left);
int right = depth(root.right);
if(left==right){
return root;
}else if(left>right){
return lcaDeepestLeaves(root.left);
}else if(left<right){
return lcaDeepestLeaves(root.right);
} return null;
}
public int depth(TreeNode node){
if(node==null) return 0;
return 1 + Math.max(depth(node.left),depth(node.right));
} }
Leetcode之深度优先搜索(DFS)专题-1123. 最深叶节点的最近公共祖先(Lowest Common Ancestor of Deepest Leaves)的更多相关文章
- [LeetCode] 1123. Lowest Common Ancestor of Deepest Leaves 最深叶结点的最小公共父节点
Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall that: Th ...
- LeetCode 1123. Lowest Common Ancestor of Deepest Leaves
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/ 题目: Given a rooted b ...
- 1123. Lowest Common Ancestor of Deepest Leaves
link to problem Description: Given a rooted binary tree, return the lowest common ancestor of its de ...
- 【leetcode】1123. Lowest Common Ancestor of Deepest Leaves
题目如下: Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall th ...
- Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences)
Leetcode之深度优先搜索&回溯专题-491. 递增子序列(Increasing Subsequences) 深度优先搜索的解题详细介绍,点击 给定一个整型数组, 你的任务是找到所有该数组 ...
- Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III)
Leetcode之深度优先搜索&回溯专题-980. 不同路径 III(Unique Paths III) 深度优先搜索的解题详细介绍,点击 在二维网格 grid 上,有 4 种类型的方格: 1 ...
- Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game)
Leetcode之深度优先搜索&回溯专题-679. 24 点游戏(24 Game) 深度优先搜索的解题详细介绍,点击 你有 4 张写有 1 到 9 数字的牌.你需要判断是否能通过 *,/,+, ...
- Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers)
Leetcode之深度优先搜索&回溯专题-638. 大礼包(Shopping Offers) 深度优先搜索的解题详细介绍,点击 在LeetCode商店中, 有许多在售的物品. 然而,也有一些大 ...
- 深度优先搜索(DFS)专题讲座PPT截图【需要原稿的请留言或私信】
以下是今晚我在bilibili直播讲DFS算法的时候的ppt截图,ppt搞了一下午,水平有限,只能做成这个样子,供大家参考!(如果需要原稿,请在评论区留言或私信告诉我,我会发到你的邮箱里),感谢各位的 ...
随机推荐
- python课堂整理19----迭代器和生成器
一.概念 • 迭代器协议: 对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么引起一个stopIteration异常,以终止迭代(只能往后走,不能往前退) • 协议是一种约定,pyt ...
- 优化 Ubuntu
优化Ubuntu 1. 更换 apt 源 echo 'deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe mul ...
- python基础——变量
变量是只不过保留的内存位置用来存储值.这意味着,当创建一个变量,那么它在内存中保留一些空间. 根据一个变量的数据类型,解释器分配内存,并决定如何可以被存储在所保留的内存中.因此,通过分配不同的数据类型 ...
- 如何在docker下安装elasticsearch(上)
一 环境 VMware® Workstation 15 Pro centos7 (1810) docker19.03.1 二 进入centos7启动dcoker systemctl start doc ...
- C#:正则表达式类
Regex r = new Regex("abc"); // 定义一个Regex对象实例(Regex r = new Regex("abc", RegexOp ...
- 使用 Docker 生成 Let’s Encrypt 证书
概念 什么是 Container ? https://www.docker.com/resources/what-container https://www.docker.com/why-docker ...
- 并发编程(4)——AbstractQueuedSynchronizer
AQS 内部类Node 等待队列是CLH有锁队列的变体. waitStatus的几种状态: static final int CANCELLED = 1; /** waitStatus value t ...
- dotnetcore 与 hbase 之二——thrift 客户端的制作
说明 在上一篇文章dotnetcore 与 hbase 之一--hbase 环境准备结束后,我们已经有了 hbase 数据库环境.接下来就可以利用 thrift 生成 c# hbase 客户端了.如果 ...
- 消息中间件-activemq实战之整合Spring(四)
前面的理论准备已经很充分,这一节我们来实战:将activemq整合到Spring框架才行中,因为Spring已经集成了JMS,这也为我们配置activermq带来了方便. 1. Spring对jms的 ...
- 自定义markdown代码高亮显示-cnblog
这个代码高亮..一点儿都不高亮...... cnblog里已经有闻道先者贴出代码了, https://www.cnblogs.com/liutongqing/p/7745413.html 效果大概是这 ...