671. Second Minimum Node In a Binary Tree

iOS自动化之打包-xcodebuild命令行打包.app并安装到ios模拟器 xcode xcodebuild xcrun ios模拟器 iOS UI自动化测试 ios自动打包安装到ios模拟器 阅读详情

1. 原题

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.

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:
2
/ \
2 5
/ \
5 7

Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.
Example 2:
Input:
2
/ \
2 2

Output: -1
Explanation: The smallest value is 2, but there isn’t any second smallest value

2. 分析

  • 每个节点的值是左右子叶的一个最小值, 所以整棵树的根节点一定是最小的
  • 如果一个节点的左子树空, 也就是说这个节点只能由右子树产生, 反之一样
  • 如果一个节点和父节点的值一样, 继续分左右搜索, 不一样表示已经找到了候选值。因为到了这一层表示父节点所在的那一层的数和根节点一样, 所以这个节点需要和兄弟节点比较选一个小的就可以了

3. 代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int getSecond(TreeNode* root, int min){
        if (!root) return -1;
        /*
        *这里比较重要, 当发现有不等于根节点的数出现, 就可以返回传给上层函数(父节点的getSecond中的left 或者 right).
        */
        if (root->val != min) return root->val;
        int left = getSecond(root->left, min);
        int right = getSecond(root->right, min);
        if (left == -1) return right;
        if (right == -1) return left;
        return left>right?right:left;
    }

    int findSecondMinimumValue(TreeNode* root) {
        if (!root) return -1;
        int min = root->val;
        int ans = getSecond(root, min);
        return ans;
    }
};
基于微信小程序的答题系统 学习视频:《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!5812)]《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取! 阅读详情

相关推荐

Gemini Enterprise免费试用攻略:解锁企业级AI平台的隐藏技巧与避坑指南

本文详细介绍了Gemini Enterprise免费试用的全流程攻略,包括注册优化、团队协作、资源管理和典型应用场景。通过解锁企业级AI平台的核心功能如Gemini 3.0 Pro模型和Nano Banana图像生成系统,帮助企业高效评估AI与业务的契合度。文章还提供了安全合规与数据管理的实用建议,确保试用过程顺畅无忧。

weixin_29233857的博客 657

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...

珍妮的选择的博客 264

简单搭建 可视化日志+链路追踪

JAVA 可视化日志、链路追踪、plumeLog、Tlog、Logback

qq_39535439的博客 1204

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

Prokka实战:10分钟搞定细菌基因组注释(附详细参数解析)

本文深入解析Prokka细菌基因组注释工具的核心参数与实战策略。通过拆解其集成化工作流程,详细解读物种分类、功能注释、输出控制等关键参数,并提供快速初筛、高质量发表、质粒注释及批量处理四种典型场景的高效参数组合方案,帮助用户精准掌控注释过程,将原始序列转化为有价值的生物学见解。

weixin_29271053的博客 442

Grafana图像渲染插件安装全攻略:解决依赖缺失问题(附完整命令清单)

本文详细介绍了在CentOS/RHEL环境下安装Grafana图像渲染插件`grafana-image-renderer`的全过程,包括解决常见的依赖缺失问题。通过提供完整的命令清单和疑难问题排查指南,帮助用户高效部署和优化插件性能,确保图像渲染功能稳定运行。

weixin_30399821的博客 85

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

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

Made in Code 1383

别再只当代码阅读器了!手把手教你用Understand的实体定位器和信息浏览器,5分钟精准定位代码逻辑

本文深入解析Understand代码分析工具的高级功能,重点介绍实体定位器和信息浏览器的实战应用。通过5分钟快速定位代码逻辑的技巧,帮助开发者从代码阅读者升级为逻辑侦探,提升大型代码库的分析效率。文章包含过滤搜索、调用链追踪等实用方法,是掌握Understand核心功能的必备指南。

weixin_28745821的博客 292

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 twoor zero sub-node. If the node has two sub-nodes, then this

zhaihaixu的博客 598

【Matlab】飞行器气动弹性仿真与分析

气动弹性是飞行器设计领域的核心交叉学科,融合了空气动力学、结构动力学和控制理论,主要研究飞行器结构在气动力作用下的弹性变形、振动响应及相互耦合规律,直接影响飞行器的飞行稳定性、操纵性和结构安全性。随着飞行器向高速度、高机动性、轻量化方向发展,气动弹性问题愈发突出,如颤振、发散、嗡鸣等现象,严重时会导致飞行器结构破坏甚至飞行事故,因此气动弹性仿真与分析已成为飞行器设计、验证和优化的关键环节。当前,飞行器气动弹性仿真面临诸多技术挑战:气动力与结构变形的强耦合特性难以精准描述,易出现仿真误差;

unber的博客 233

【制作100个Unity游戏之36】用unity从零开始实现类《开心消消乐》的三消类消除解压小游戏

Unity甜品消除游戏开发摘要 本文介绍了使用Unity开发甜品消除游戏的核心实现步骤: 冰块阵列生成 通过GlobalDef定义全局参数(9行7列网格) IceSpawner脚本动态生成网格化冰块阵列 添加Resolution脚本实现多分辨率适配 随机水果生成 创建FruitSpawner作为水果容器 FruitItem脚本管理水果属性(行列索引、类型) 支持动态创建水果精灵并设置位置 关键技术点 使用Instantiate动态生成预制体 通过localPosition精确控制网格布局 分辨率适配算法保持

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 1289

[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...

闲庭信步 330

ransac拟合2D圆(三点定圆 c++详细版)

RANSAC(Random Sample Consensus,随机采样一致性)算法是一种基于随机采样的迭代算法,主要用于从一组包含大量噪声和异常值(外点)的数据中估计数学模型的参数。

twnkie的博客 1047

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

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

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...

537

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

LeetCode 671 Second Minimum Node In a Binary Tree?

given a tree which each val of the nodes are positive. output the second minimum node in a binary tree… pay attention: the second minimum should be the value larger than smallest if we can’t find somrthing like this then we return -1; in order to get the s

Tech in Pieces 219

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

[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 [Easy]

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 value, where each node in this tree has exactlytwoorzerosub-node. If the node has two sub-nodes, then t...

Young's Blog 107

leetcode 671 Second Minimum Node In a Binary Tree

Java class Solution { public int findSecondMinimumValue(TreeNode root) { if(root==null) return -1; if(root.left==null && root.right==null) return -1; int left=roo

MRxjh的博客 343
上一篇: 230. Kth Smallest Element in a BST
下一篇: 102. Binary Tree Level Order Traversal
lifi_sysu
博客等级 码龄9年 0粉丝 20原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值