/**
* 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. unity实现3D物体上的事件监听处理

    想要在3D物体上实现全套事件监听处理: OnMouse系列 OnTrigger系列 OnPointer系列 OnDrag系列   1.在相机中添加Physics Raycaster组件 2.3D物体上 ...

  2. WLC5520无法通过无线客户端进行网管故障解决

    客户反馈其办公环境中的WLC5520网管需要通过内部有线网络进行管理,通过无线客户端无法进行管理,远程协助其开启WLC5520的无线管理功能后故障解决.

  3. 用户管理系统之class

    接着上一篇博客继续往下总结,上一篇博客的地址:https://www.cnblogs.com/bainianminguo/p/9189324.html 我们开始吧 这里我们就需要先看下我们设计的数据库 ...

  4. SQL Server2005/2008 作业执行失败的解决办法

    数据库:SQL Server 2005/2008,运行环境:Windows Server 2008  在数据库里的所有作业都执行失败,包括自动执行和手动执行.在事件查看器里看到的错误报告如下: 该 作 ...

  5. Java 枚举类 详解

    1.枚举是什么? Java中的枚举其实是一种语法糖,在 JDK 1.5之后出现,用来表示固定且有限个的对象.比如一个季节类有春.夏.秋.冬四个对象:一个星期有星期一到星期日七个对象.这些明显都是固定的 ...

  6. SVO+PL-SVO+PL-StVO

    PL-SVO是基于点.线特征的半直接法单目视觉里程计,我们先来介绍一下基于点特征的SVO,因为是在这个基础上提出的. [1]References:      SVO: Fast Semi-Direct ...

  7. Android开发日常-listVIiew嵌套webView回显阅读位置

     详情页布局结构 需求是回显webview展示网页的阅读位置 方案1: 使用webview.getScrollY()获取滑动到的位置,用setScrollY()回显设置, 但是两个方法都出现了问题,g ...

  8. Oracle性能优化4-索引

    Oracle优化可以分为通过改写sql优化和不改写sql优化不改写sql优化一般通过index来实现 在Oracle数据库中,索引按照索引机制的不同,可以分为三种. 1. B-Tree索引 B-Tre ...

  9. Java.Annotations

    Annotation 0. Annotation Tricks http://developer.android.com/reference/java/lang/annotation/Annotati ...

  10. 使用Tophat+cufflinks分析差异表达

    使用Tophat+cufflinks分析差异表达  2017-06-15 19:09:43     522     0     0 使用TopHat+Cufflinks的流程图 序列的比对是RNA分析 ...