/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public:
int mindiff=INT_MAX;
int findSecondMinimumValue(TreeNode* root)
{
travelfind(root);
return (mindiff==INT_MAX? -:mindiff+root->val);
} void travelfind(TreeNode* root)
{
int rnum=root->val;
if(root->left!=NULL)
{
if(rnum==root->left->val)
travelfind(root->left);
else
mindiff=min(mindiff,root->left->val-rnum);
if(rnum==root->right->val)
travelfind(root->right);
else
mindiff=min(mindiff,root->right->val-rnum);
}
else
return;
}
};

递归,不要慌,问题不大。子节点一定比父节点大,那么根节点为最小值,

设置一个全局差值变量

只要子节点值和根节点相等,则继续向下查找,否则计算差值,保留最小差值

这样查找,不用遍历完整棵树,只需要遍历完节点值和根节点值相等的所有节点及其子节点即可

返回值为最小差值和根节点值之和

671. Second Minimum Node In a Binary Tree的更多相关文章

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

  2. 【easy】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 no ...

  3. [LeetCode&Python] Problem 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 no ...

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

  5. 671. Second Minimum Node In a Binary Tree 非递减二叉树中第二小的元素

    [抄题]: Given a non-empty special binary tree consisting of nodes with the non-negative value, where e ...

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

  7. 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 找出所有值再求次小值 遍历时求次小值 日期 题目地址 ...

  8. Python 解LeetCode:671. Second Minimum Node In a Binary Tree

    题目在这里,要求一个二叉树的倒数第二个小的值.二叉树的特点是父节点的值会小于子节点的值,父节点要么没有子节点,要不左右孩子节点都有. 分析一下,根据定义,跟节点的值肯定是二叉树中最小的值,剩下的只需要 ...

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

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

随机推荐

  1. C#判断文件和文件夹是否存在 不存在则创建

    using System.IO;string path = @"D:\accountDaoRu\";        if (Directory.Exists(path) == fa ...

  2. 流(Stream)、文件(File)和IO

    读取控制台输入 Java 的控制台输入由 System.in 完成. 为了获得一个绑定到控制台的字符流,你可以把 System.in 包装在一个 BufferedReader 对象中来创建一个字符流. ...

  3. Linux磁盘挂载详述

    1.查看硬盘信息及分区 一般使用”fdisk -l”命令可以列出系统中当前连接的硬盘,设备和分区信息.新硬盘没有分区信息,则只显示硬盘大小信息. [root@localhost home]# fdis ...

  4. Bootstrap(6)图标菜单按钮组件

    一.小图标组件 Bootstrap 提供了免费的 263 个小图标(数了两次),具体可以参考中文官网的组件链接:http://v3.bootcss.com/components/#glyphicons ...

  5. position的absolute与fixed,absolute与relative共同点与不同点

    absolute与fixed 共同点: (1) 改变行内元素的呈现方式,display被置为block: (2) 让元素脱离普通流,不占据空间: (3) 默认会覆盖到非定位元素上 不同点: absol ...

  6. XSS 攻击的防御

    xss攻击预防,网上有很多介绍,发现很多都是只能预防GET方式请求的xss攻击,并不能预防POST方式的xss攻击.主要是由于POST方式的参数只能用流的方式读取,且只能读取一次,经过多次尝试,自己总 ...

  7. DNS的过程

    来自:https://www.zhihu.com/question/23042131 作者:郭无心链接:https://www.zhihu.com/question/23042131/answer/6 ...

  8. Solidity语言基础 和 Etherum ERC20合约基础

    1. 类型只能从第一次赋值中推断出来,因此以下代码中的循环是无限的,  小. for (var i = 0; i < 2000; i++) { ... } --- Solidity Types ...

  9. 在ubuntu16.04编译安装httperf

    在网上偶尔看到httperf可以在测试中使用.网上苦苦找不到ubuntu编译安装的方法.自己琢磨了一下,现在总结如下: apt install httperf vim /etc/security/li ...

  10. PAT 1036 跟奥巴马一起编程(15)(代码)

    1036 跟奥巴马一起编程(15)(15 分) 美国总统奥巴马不仅呼吁所有人都学习编程,甚至以身作则编写代码,成为美国历史上首位编写计算机代码的总统.2014年底,为庆祝"计算机科学教育周& ...