问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4100 访问。

给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值。

给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。

输入: 

2

   / \

  2   5

       / \

     5   7

输出: 5

说明: 最小的值是 2 ,第二小的值是 5 。

输入:

2

    / \

  2   2

输出: -1

说明: 最小的值是 2, 但是不存在第二小的值。


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.

Input: 

2

   / \

  2   5

       / \

     5   7

Output: 5

Explanation: The smallest value is 2, the second smallest value is 5.

Input: 

2

    / \

  2   2

Output: -1

Explanation: The smallest value is 2, but there isn't any second smallest value.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4100 访问。

public class Program {

    public static void Main(string[] args) {
var root = new TreeNode(1) {
left = new TreeNode(3) {
left = new TreeNode(5),
right = new TreeNode(7)
},
right = new TreeNode(9)
}; var res = FindSecondMinimumValue(root);
Console.WriteLine(res); Console.ReadKey();
} public static int FindSecondMinimumValue(TreeNode root) {
var first = int.MaxValue;
var second = int.MaxValue;
PreOrder(root, ref first, ref second);
return second != int.MaxValue ? second : -1;
} public static void PreOrder(TreeNode root, ref int first, ref int second) {
//前序遍历
if(root == null) return;
if(root.val < first) {
//如果当前值更比 first 还小
//那么第 2 小变成当前最小
//当前最小变成当前值
second = first;
first = root.val;
} else if(root.val > first && root.val < second)
//如果界面 2 者之间
//将第 2 小变成当前值即可
second = root.val;
PreOrder(root.left, ref first, ref second);
PreOrder(root.right, ref first, ref second);
} public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
} }

以上给出1种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4100 访问。

3

分析:

显而易见,以上算法的时间复杂度为:  。

C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)的更多相关文章

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

    671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 ...

  2. [Swift]LeetCode671. 二叉树中第二小的节点 | Second Minimum Node In a Binary Tree

    Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...

  3. Java实现 LeetCode 671 二叉树中第二小的节点(遍历树)

    671. 二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的 ...

  4. Leetcode 671.二叉树中第二小的节点

    二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树 ...

  5. [LeetCode] 671. 二叉树中第二小的节点 ☆(递归 合并)

    描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有 ...

  6. [LeetCode]671. 二叉树中第二小的节点(递归)

    题目 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有 ...

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

    题目: Given a non-empty special binary tree consisting of nodes with the non-negative value, where eac ...

  8. LeetCode671. 二叉树中第二小的节点

    题目 纯暴力 1 class Solution { 2 public: 3 vector<int>ans; 4 int findSecondMinimumValue(TreeNode* r ...

  9. 【Leetcode_easy】671. Second Minimum Node In a Binary Tree

    problem 671. Second Minimum Node In a Binary Tree 参考 1. Leetcode_easy_671. Second Minimum Node In a ...

随机推荐

  1. Spirngboot-自定义Starter

    一.Spring Boot Starter简介 Starter是Spring Boot中的一个非常重要的概念,Starter相当于模块,它能将模块所需的依赖整合起来并对模块内的Bean根据环境( 条件 ...

  2. [jvm] -- 内存模型篇

    内存模型 JDK1.6  JDK1.8  线程私有的: 程序计数器 虚拟机栈 本地方法栈 线程共享的: 堆 方法区 直接内存 (非运行时数据区的一部分) 程序计数器 线程私有 两个作用 字节码解释器通 ...

  3. js JQ动态添加div标签

    function renderList(data){ var str = ''; for(var i = 0; i < data.length; i++){ // 动态添加li str += ' ...

  4. Python编程入门(第3版)|百度网盘免费下载|零基础入门学习资料

    百度网盘免费下载:Python编程入门(第3版) 提取码:rsd7 目录  · · · · · · 第1章 编程简介 11.1 Python语言 21.2 Python适合用于做什么 31.3 程序员 ...

  5. BUUCTF-web ikun(Python 反序列化)

    正如本题所说,脑洞有点大.考点还很多,不过最核心的还是python的pickle反序列化漏洞 题目中暗示了要6级号,找了很多页都没看到,于是写了脚本 在第180页有6级号,但是价格出奇的高,明显买不起 ...

  6. 【Python】Async异步等待简单例子理解

    import time def run(coroutine): try: print("") coroutine.send(None) except StopIteration a ...

  7. Spring+hibernate+JSP实现Piano的数据库操作---3.Piano实体类

    package com.entity; import org.springframework.stereotype.Component; import javax.persistence.*; @Co ...

  8. 使用vuex做列表数据过滤

    功能需求 展示一个表格,表格包含选项有" 姓名 年龄 是否显示"三个列表项 是否显示列表项是可操作开关,点击切换打开/关闭 将表格中开关为打开状态的列表项,在另一个表格中显示 需求 ...

  9. vue“欺骗”ueditor,实现图片上传

    一.环境介绍 @vue/cli 4.3.1 webpack 4.43.0 ueditor1.4.3.3 jsp版 二.springboot集成ueditor,实现分布式图片上传 参考我的另一篇博客,& ...

  10. Day07_品牌管理

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 0.学习 ...