leetcode 671. Second Minimum Node In a Binary Tree(二叉树中第二大的值)

LeetCode --- 671. Second Minimum Node In a Binary Tree 解题报告 Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactlytwoorzerosub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. More f... 阅读详情

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node’s value is the smaller value among its two sub-nodes. More formally, the property root.val = min(root.left.val, root.right.val) always holds.

Given such a binary tree, you need to output the second minimum value in the set made of all the nodes’ value in the whole tree.

If no such second minimum value exists, output -1 instead.

Example 1:
在这里插入图片描述
Input: root = [2,2,5,null,null,5,7]
Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.

Constraints:

The number of nodes in the tree is in the range [1, 25].
1 <= Node.val <= 231 - 1
root.val == min(root.left.val, root.right.val) for each internal node of the tree.

给出一个特殊的二叉树,每个节点只有0 or 2个节点,且root的值是左右子树值中较小的那个。
找出第二大的值,如果没有就返回-1

思路:
因为root的值是左右子树中较小的,所以整个二叉树的最小值就是root的值。
刚开始想在左右子树中找到比root大的第一个值就立刻返回,但是发现,假如root是1,左子树是1,右子树是3,如果立刻返回就是3。但是左子树1的下面可能还有2呢。所以要把3保存下来,但是还要遍历1下面的节点,不断地更新第二大的值,可以用DFS实现。
比当前结果还要大的节点就不需要再遍历下去了。

因为值的范围可以到达int型的最大值,所以先用long型最大值,而后更新BS中第二大的值。最后要cast到int。

class Solution {
    long result = Long.MAX_VALUE;
    public int findSecondMinimumValue(TreeNode root) {
        if(root == null) return -1;
        int minV = root.val;
        helper(root, minV);
        return (int)(result == Long.MAX_VALUE ? -1 : result);
    }
    
    void helper(TreeNode root, int minV) {
        if(root == null) return;
        if(root.val > minV && root.val < result) {
            result = root.val;
        } else if(root.val == minV) {
            helper(root.left, minV);
            helper(root.right, minV);
        }
    }
}
二叉树中找出第i大的元素 在二叉树中查找出度为2的节点个数,可供参考 立即下载

相关推荐

Java:使用Java调用打印机进行打印(JPG、PDF和Word三种文件格式)

目录 一、Java的打印简介 二、Java打印实现 2.1 JPG图片文件格式打印实现 2.2 PDF文件格式打印实现 2.3 Word文件格式打印实现 2.3.1 Word文件采用jacob插件进行打印实现。 2.3.2 先将word转化为pdf文件,然后打印pdf(lz使用) 三、总结 一、Java的打印简介 在我们的实际工作中,经常需要实现打印功能。但由于历史原因,Jav...

vatxiongxiaohui的专栏 8万+

leetcode 671. Second Minimum Node In a Binary Tree

方法一:用一个set存储遍历二叉树的值,很常规的方法。 方法二:用递归。一开始屡不清关系,总感觉这个函数的返回值是第二小的值呀,怪怪的,这可怎么返回,我再过程中调用的时候是需要它返回子树中最小的呀。其实设定递归的条件就可以,如果当前子节点的值与父节点的值相等那么就递归下去,返回的虽然是子树的第二小的值,但也是对应的想要的子树的最小值。 class Solution { public int...

xiaobailaji的博客 328

金蝶云星空核心数据表解析与高效关联查询指南

本文深入解析金蝶云星空核心数据表结构,提供高效关联查询的实战指南。文章详细介绍了基础资料、业务单据和财务模块的关键表及其关联逻辑,重点阐述了如何利用关联表(_LK表)进行跨单据溯源,并分享了编写高性能SQL的实用技巧与避坑建议,帮助开发者、实施顾问和数据分析师快速掌握数据查询方法,提升工作效率。

weixin_29254673的博客 575

找出二叉搜索树的第2大的数

找出二叉搜索树的第2大的数 二叉搜索树:树的左侧都比根节点小,树的右侧都比根节点大,树的子树也是如此 package solution35; ​ public class Solution { public int solution(TreeNode root) { TreeNode node = root; while(root.right !...

qq_35828475的博客 728

ROS2机械臂实战:从基础控制到视觉抓取

本文详细介绍了ROS2机械臂从仿真环境搭建到视觉抓取的全流程实战指南。内容涵盖使用Gazebo搭建仿真环境、编写基础控制节点、集成视觉感知进行目标检测与坐标变换,并设计状态机逻辑实现自动化抓取。通过系统集成与联调,帮助开发者掌握ROS2机械臂开发的核心技能。

weixin_29197699的博客 467

基于NXP1778的CAN自动波特率检测

一、概念 NXP1778芯片内部自带一个CAN控制器。CAN模块由两部分组成:控制器和验收滤波器。所有的寄存器和RAM都以32位字宽度来访问。 CAN自动波特率检测主要用于以一种CAN的转换的产品中。产品中CAN部分的波特率以前都是通过网页中修改然后配置下去的,依据实际情况的考虑,如果CAN部分的波特率能够根据客户设备的改变而自动改变,将会增加其灵活性。   二、原理 根据CAN控制器的

菜鸟的痕迹专栏 5287

hdu-1710

Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18570Accepted Submission(s): 7672 Problem Description A binary tree is a finite set of vertices that is either empty or...

qq_42273272的博客 222

cut&tag学习笔记

上图是我得到的结果,下图是教程的结果,比对结果有一些差异,我也不知道具体原因是啥。谷歌了一下,发现原因可能是因为环境变量的问题,系统不知道picard的具体位置,需要设置全局环境变量,遂查了一下picard的位置。我发现我做出来的结果测序深度一致,但是比对的结果有一点差异,不知道是我的操作问题,还是参考基因组更新了导致比对结果出现了些许不同。上图是我得到的结果,下图是教程的结果,除了IgG_rep1外比对结果一致。上图是我的结果,下图是教程的结果,结果不太一致,不知道是具体的原因是什么。

weixin_52073820的博客 2437

寻找二叉树的第k大节点

需求: 给定一棵二叉树,求出第k大的节点。 根据二叉树的中序遍历中序遍历即可找到第k大的节点。 实现: 节点定义:typedef struct node{ int data; struct node *left; struct node *right; }BTNode; BTNode* searchTagCore(BTNode *root, int* k){

xiao-ren-wu的博客 2626

ATAC-seq数据分析流程学习记录及总结

记录ATAC-seq数据分析流程,主要是参考健明老师的教程以及网上各种学习资源进行总结,网址在下面。

ACGTexplorer的博客 3772

5分钟搞定!用MCU GPIO实现零功耗一键开关机电路(附完整原理图)

本文详细介绍了如何利用MCU GPIO设计零功耗一键开关机电路,适用于便携式设备和电池供电项目。通过软硬件结合的方式,实现关机状态下的真正零功耗,同时提供智能开关机控制。文章包含完整原理图、元件选型指南和实际搭建技巧,帮助开发者在5分钟内完成高效电源管理方案。

weixin_28339967的博客 354

671.second-minimum-node-in-a-binary-tree

给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么该节点的值等于两个子节点中较小的一个。 更正式地说,root.val = min(root.left.val, root.right.val) 总成立。 给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。 示例 1: 输入: root = [2,2,5,null,null,5,7] 输出: 5 解释: 最小的值是 2 ,第二小的值是 5 。

qq_26000415的博客 243

UNet改进(54):当医学图像分割遇上频域学习与自监督

本文提出了一种改进U-Net的方法,通过频域增强模块和代理任务模块提升医学图像分割性能。频域增强模块在傅里叶空间对特征图进行可学习的频率调制,重点增强高频细节特征;代理任务模块通过图像重建约束编码器保留关键信息。两个模块协同工作,在仅增加5%参数量的情况下,形成空间域、频域和重建约束三条互补的信息路径。该方法特别适用于需要精细边缘分割的医学图像任务,预期能提升5%-10%的边缘分割精度,在小样本场景效果更显著。部署时可移除代理任务模块,不影响推理效率。未来可探索空间自适应频域调制和更丰富的代理任务设计。

2501_91516851的博客 233

LeetCode //C - 671. Second Minimum Node In a Binary Tree

【代码】LeetCode //C - 671. Second Minimum Node In a Binary Tree。

Made in Code 1395

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

VB摄像头监控

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

Leetcode 671. Second Minimum Node In a Binary Tree

Leetcode 671. Second Minimum Node In a Binary Tree Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node....

fengran_mark的博客 251

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LeetCode 671. Second Minimum Node In a Binary Tree

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's va...

539

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

Leetcode 671.二叉树中第二小的节点(Second Minimum Node In a Binary Tree)

Leetcode 671.二叉树中第二小的节点 1 题目描述(Leetcode题目链接)   给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值。 给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。 输入: 2 / \ 2 5 / \ 5 7 输出: 5 说明: 最小的值是 2 ,第二小的值是 5 。 输入:

qq_39378221的博客 213

[leetcode] 671. Second Minimum Node In a Binary Tree @ python

原题 Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node’s...

闲庭信步 331

671. Second Minimum Node In a Binary Tree*

671. Second Minimum Node In a Binary Tree* https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/ 题目描述 Given a non-empty special binary tree consisting of nodes with the non-negative valu...

珍妮的选择的博客 266

LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值。 给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1。 每日一算法2019/...

weixin_30852451的博客 110

Leetcode 671. Second Minimum Node In a Binary Tree(二叉树中第二小的节点)

Second Minimum Node In a Binary Tree Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node ...

秀秀的奇妙旅行 267

[leetcode] 671. Second Minimum Node In a Binary Tree

Description Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then th...

Learning from the mistakes 156

671 Second Minimum Node In a Binary Tree

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's va...

dongbeier的博客 177

LeetCode 671. Second Minimum Node In a Binary Tree(二叉树中第二小的节点)

给定一个非空特殊的二叉树,每个节点都是正数,并且**每个节点的子节点数量只能为 2 或 0**。如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值。 root.val = min(root.left.val, root.right.val) 给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。 示例 1: 输入: 2 ...

肉丸不肉 458
上一篇: leetcode 669. Trim a Binary Search Tree(修剪二叉搜索树)
下一篇: leetcode 684. Redundant Connection(多余的连接)
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

蓝羽飞鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值
⌂ 换网址