leetcode 865. Smallest Subtree with all the Deepest Nodes(包含最深点的最小子树)

从论文到生产:iSLIP优先级匹配算法在SDN交换机中的20年演进史 本文回顾了iSLIP优先级匹配算法在SDN交换机中的20年演进历程,从斯坦福大学的原始论文到现代芯片实现。通过独特的指针滑动和迭代循环机制,iSLIP算法显著提升了交换机的吞吐量和公平性,解决了传统FIFO调度的队头阻塞问题。文章详细探讨了算法在硬件实现中的工程挑战、开源实践及未来AI时代的智能调度方向。 阅读详情

Given the root of a binary tree, the depth of each node is the shortest distance to the root.

Return the smallest subtree such that it contains all the deepest nodes in the original tree.

A node is called the deepest if it has the largest depth possible among any node in the entire tree.

The subtree of a node is tree consisting of that node, plus the set of all descendants of that node.

Note: This question is the same as 1123: https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/

Example 1:
在这里插入图片描述
Input: root = [3,5,1,6,2,0,8,null,null,7,4]
Output: [2,7,4]
Explanation: We return the node with value 2, colored in yellow in the diagram.
The nodes coloured in blue are the deepest nodes of the tree.
Notice that nodes 5, 3 and 2 contain the deepest nodes in the tree but node 2 is the smallest subtree among them, so we return it.

找出包含最深点的最小子树,最深点是指到root的距离最远。

思路:
可按求树的高度计算,但是返回高度的同时还要返回包含最深点的最小树的root
如果左右子树的高度相同,那么当前root就可能是最小树的root
如果左子树高度>右子树高度,那当前root就不是最小树的root,最小树在左子树上,返回时root就是左子树返回的root

因为又要返回高度,又要返回最小树的root,可以定义一个class,包含这两个成员,当然也可以返回一个Object数组,取值时用down cast

    public TreeNode subtreeWithAllDeepest(TreeNode root) {
        if(root == null) return null;
        return (TreeNode)(height(root)[1]);
    }
    
    Object[] height(TreeNode root) {
        if(root == null) {
            return new Object[]{-1, root};
        }
        
        Object[] left = height(root.left);
        Object[] right = height(root.right);
        
        int leftH = ((Integer)left[0]).intValue();
        int rightH = ((Integer)right[0]).intValue();
        
        if(leftH > rightH) {
            return new Object[]{leftH+1, left[1]};
        } else if(leftH < rightH) {
            return new Object[]{rightH+1, right[1]};
        }
        
        return new Object[]{leftH+1, root};
    }
多物理场耦合仿真-主题032-电磁-流体耦合 电磁-流体耦合,即磁流体动力学(MHD, Magnetohydrodynamics),是研究导电流体在电磁场中运动规律的学科,涉及电磁场与流体力学的深度耦合。本主题系统阐述磁流体动力学的基本理论、控制方程和数值方法。首先介绍MHD的基本概念和物理基础,包括导电流体的性质、电磁场基本方程;然后详细推导MHD控制方程组,包括连续性方程、动量方程、能量方程和电磁场方程;接着讨论MHD流动的特征参数,如Hartmann数、磁雷诺数、相互作用参数等;最后通过六个典型工程案例展示MHD分析的实际应用,包括电磁泵、磁流体 阅读详情

相关推荐

CXL3.0 CDAT(Coherent Device Attributes Table)

在说明CDAT之前,先讲一下CXL中的DOE类型:CXL协议里定义了两种DOE的类型,一种是Compliance就是合规性测试的内容,还有一种就是CDAT(一致性属性表)Memory_Info_Valid是CXL设备的内存配置完成标志,位于DVSEC(Designated Vendor-Specific Extended Capability)的CXL Range X Size Low寄存器中(X为1或2)。

Summer的博客 1448

LeetCode //C - 865. Smallest Subtree with all the Deepest Nodes

Article Summary: The task is to find the smallest subtree in a binary tree that contains all deepest nodes. Using a depth-first search (DFS), for each subtree, the maximum depth and the lowest common ancestor (LCA) containing all deepest nodes are returned

Made in Code 1950

IEEE论文投稿必看:PDF eXpress报错font not embedded的3种修复方法(附Adobe Acrobat DC操作截图)

本文针对IEEE论文投稿时PDF eXpress系统常见的“font not embedded”报错,提供了三种核心修复方案。首先,详细解析了使用Adobe Acrobat DC进行专业修复的完整操作流程与关键技巧。其次,介绍了无需付费软件的开源与免费工具链(如Ghostscript)解决方案。最后,从源头出发,指导LaTeX和Microsoft Word用户在生成PDF时正确设置以确保字体自动嵌入,帮助研究者彻底解决格式问题,确保投稿顺利。

ujm56789012的博客 526

LeetCode 865. Smallest Subtree with all the Deepest Nodes解题报告(python)

865. Smallest Subtree with all the Deepest Nodes Smallest Subtree with all the Deepest Nodes python solution 题目描述 Given a binary tree rooted at root, the depth of each node is the shortest distance ...

Orientliu96的博客 336

避坑指南:Qgis等时圈分析中ORS插件Key申请与参数设置的常见错误

本文详细解析了QGIS等时圈分析中ORS插件从API密钥申请到参数优化的全流程避坑指南。针对密钥激活、出行模式选择、时空参数设置等七个高频故障点,提供了实用的调试方案和优化技巧,帮助用户高效完成空间可达性分析。

weixin_42546508的博客 315

Bochs源码分析 - 4: 使用ida来调试bochs

前言 bochs内部提供了一个调试器,但是这个命令行调试器相当难用,想找一个gui的debugger。第一次是在百度百科的bochs词条中介绍的peter-bochs-debugger调试器(java编写),但是后来发现ida也有一个bochs调试器,因为对ida比较熟悉,就选用此作为bochs调试器。 在配置过程中参考博客 https://blog.csdn.net/cssxn/article/details/90738498 中的配置,但是这是直接发布版本,拿源码编译...

WriteAnything_的博客 2768

LeetCode 865. Smallest Subtree with all the Deepest Nodes【树,DFS,BFS,哈希表】1534

本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。

memcpy0的博客 698

Python自然语言处理 NLTK 库用法入门教程【经典】

@本文来源于公众号:csdn2299,喜欢可以关注公众号 程序员学府 本文实例讲述了Python自然语言处理 NLTK 库用法。分享给大家供大家参考,具体如下: 在这篇文章中,我们将基于 Python 讨论自然语言处理(NLP)。本教程将会使用 Python NLTK 库。NLTK 是一个当下流行的,用于自然语言处理的 Python 库。 那么 NLP 到底是什么?学习 NLP 能带来什么好处? ...

python函数基础教程 1万+

leetcode_865. Smallest Subtree with all the Deepest Nodes

https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 给定一颗二叉树,输出包含所有最深叶子结点的最小子树的根节点。 解法一: 先使用dfs计算最大深度deep和最大深度的叶子结点数cnt,然后后序遍历,对每个节点再使用dfs计算以该节点为根节点的子树中所含最大深度的节点数,最先找到的最大深度为节点...

weixin_33704591的博客 157

在 ABAP CDS View 里把 Conversion Functions 用到极致:FLTP 精度、单位换算、货币换算与小数位处理的工程化实践

数据格式转换在真实项目中比业务逻辑更容易引发问题。ABAP CDS中的转换函数(如FLTP_TO_DEC、UNIT_CONVERSION、CURRENCY_CONVERSION和DECIMAL_SHIFT)可帮助在数据库层统一数据口径,避免应用层重复处理。这些函数支持数值精度治理、单位换算、货币转换和小数位调整,能显著提升报表性能并减少逻辑分散。通过将格式转换前移到数据模型层,可以确保服务层和UI层的数据一致性,同时降低接口传输错误风险。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 2375

语音识别:PyAudio、SoundDevice、Vosk、openai-whisper、Argos-Translate、FunASR(Python)

本文介绍了Python中常用的音频处理库PyAudio,详细解析了其核心功能、核心概念和典型工作流程。PyAudio是一个跨平台的音频I/O库,支持音频录制、播放、实时流处理和低延迟操作。核心概念包括PyAudio类(管理音频设备和流)和Stream类(代表音频流)。文章重点讲解了open()方法的关键参数、回调函数的使用方式以及典型的工作流程,包括设备查询、流配置和资源释放。PyAudio适用于需要实时音频处理的场景,如语音识别和音频效果处理。

算法工程师 712

865. 具有所有最深节点的最小子树(javascript)865. Smallest Subtree with all the Deepest Nodes

865. 具有所有最深节点的最小子树(javascript)865. Smallest Subtree with all the Deepest Nodes

ingenuou_的博客 336

第42章:BRepBuilderAPI_MakeEdge(构建边)

是 OpenCascade 中用于构建边(Edge)的类。边是几何建模中的基本元素之一,通常连接两个顶点,并且可以有不同的几何形式,如直线、圆弧、样条曲线等。提供了多种创建边的方法,包括通过给定两个顶点、通过参数化曲线、通过给定起始和结束点的方式等。方法说明示例使用两个顶点创建直线边使用Geom_Curve创建边使用两点的坐标创建边Shape()获取创建的边。

yumeiguo的博客 189

865. Smallest Subtree with all the Deepest Nodes [Medium]

https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ Given therootof a binary tree, the depth of each node isthe shortest distance to the root. Returnthe smallest subtreesuch that it containsall the deepest nodesin the origin...

Young's Blog 146

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

[leetcode] 865. Smallest Subtree with all the Deepest Nodes

Description Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A node is deepest if it has the largest depth possible among any node in the entire tree. T...

Learning from the mistakes 249

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

leetcode 865. Smallest Subtree with all the Deepest Nodes | 865.具有所有最深节点的最小子树(树的BFS,parent反向索引map)

层序遍历,记录最深层;建立树反向索引表;从最深层每个节点向上找parent,路过时,节点经过次数count++,找到最早出现count==n的祖先

寒泉 1万+

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

[Swift]LeetCode865. 具有所有最深结点的最小子树 | Smallest Subtree with all the Deepest Nodes...

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/stren...

weixin_30933531的博客 160

LeetCode - Medium - 865. Smallest Subtree with all the Deepest Nodes

Topic Tree Depth-first Search Breadth-first Search Recursion Description https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ Given the root of a binary tree, the depth of each node is the shortest distance to the root. Return the s

KISS 307

leetcode 865. Smallest Subtree with all the Deepest Nodes 具有所有最深节点的最小子树-耗时100

本文提出了一种寻找二叉树中包含所有最深节点的最小子树的算法。通过深度优先搜索(DFS)遍历所有路径,记录长度最大的路径(即到达叶节点的最长路径)。统计这些最长路径中每个节点的出现次数,出现次数等于最长路径数的节点即为所求子树的根节点。算法时间复杂度为O(n),空间复杂度为O(n),其中n为节点数。该方法高效地通过路径统计确定最小公共祖先,解决了LeetCode 865题的问题。

邹九的个人博客 246

【Leetcode】865. Smallest Subtree with all the Deepest Nodes

题目地址: https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/ 给定一棵二叉树,求这样的最小子树,使得该子树含全树所有最深的节点。 其实就是求所有最深节点的最近公共祖先。思路是DFS。在DFS的过程中更新全局最深节点的深度,并且先递归求解左右子树的最深节点的深度,这样就得到了全局最深节点的深度。接着看一下左右子树的最深深度是否都等于全局最深深度,如果是,则意味着左右子树都有最深节点,则说明当前节点可能是解(也有

数学、算法爱好者的博客 248

【LeetCode】865. Smallest Subtree with all the Deepest Nodes

Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A node is deepest if it has the largest depth possible among any node in the entire tree. The subtree...

iKun 377

【leetcode 二叉树 C++】865. Smallest Subtree with all the Deepest Nodes

865. Smallest Subtree with all the Deepest Nodes /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int

izhxxx的博客 114

LeetCode: 866. Smallest Subtree with all the Deepest Nodes

LeetCode: 866. Smallest Subtree with all the Deepest Nodes 题目描述 Given a binary tree rooted at root, the depth of each node is the shortest distance to the root. A node is deepest if it has the larg...

杨领well的专栏 542

Leetcode 866. Smallest Subtree with all the Deepest Nodes

https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/description/好想好写的写法就是两次遍历class Solution { public: int maxDep(TreeNode* root) { if (!root) return 0; return max(maxDep(r...

Pilgrim 603

LeetCode 3884.双端字符匹配:半次遍历

LeetCode 3884.双端字符匹配:半次遍历 给你一个长度为 n 的字符串 s,其中只包含小写英文字母。 返回最小的下标 i,使得 s[i] == s[n - i - 1]。 如果不存在这样的下标,返回 -1。

<font color="Pink">Tisfy的博客</font> 167

力扣 1201. 丑数 III

LeetCode 原题:1201. 丑数 III 今天的每日一题 只能说还可以,不是很难,虽然我一开始也没想到,我本来以为这个题类似归并排序的 那种,把每个数的前n个小的倍数放到一个优先对列中,不过这样有重复的,之后换成set 还是不行超时了。看到题解才恍然大悟,可以用二分和容斥原理来判断这个数前面有多少数比它小……给定四个整数 、、、。如果一个正整数能够被 、、 中的至少一个整除,那么它就是丑数。请返回第 个丑数。示例: 二、解题思路 这道题的核心是:二分答案 + 容斥原理。可以把它想象成三个社团分别招

yydschdyyds的博客 198
上一篇: leetcode 863. All Nodes Distance K in Binary Tree(二叉树中距离K的节点)
下一篇: leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal(前序和后序遍历数组复原二叉树)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址