/**
* 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. 查看linux发行版版本

    可以通过查看/etc/issue文件查看发行版版本号,不论系统是redhat\suse\debian

  2. MongoDB之增删改查

    MongoDB的默认端口为:27017 show  dbs   查看所有的数据库 MySQL和MongoDB的对应关系 MySQL MongoDB DB DB 数据库 table Collection ...

  3. checkbox/radio 样式修改

    只改颜色 input[type=radio],input[type=checkbox] { display: inline-block; vertical-align: middle; width: ...

  4. 转移动APP测试实践

    http://blog.csdn.net/hgstclyh/article/details/53115325

  5. C#创建cookie读写cookie

    一.创建cookie HttpCookie cookie = new HttpCookie("UserInfo");//创建多值cookie              cookie ...

  6. ACM-ICPC 2018 南京赛区网络预赛 L.Magical Girl Haze(分层最短路)

    There are N cities in the country, and M directional roads from u to v(1≤u,v≤n). Every road has a di ...

  7. [Java笔记]面向对象-单例模式

    单例模式 目标 使JVM中最多只有一个该类的实例,以节省内存.缺点:只能建一个该类的实例. 实现 具体实现思路: 1构造方法私有化//故在外面不能new很多次 2对外提供一个公开的静态的类方法,获取类 ...

  8. Windows Server RRAS 配置

    在Windows Server上,RRAS 是 Rounting and Remote Access Service 的简称. 通过 RRAS UI 管理器可实现 VPN 和 NAT 的配置. RRA ...

  9. 用Python监听鼠标和键盘事件

    PyHook是一个基于Python的“钩子”库,主要用于监听当前电脑上鼠标和键盘的事件.这个库依赖于另一个Python库PyWin32,如同名字所显示的,PyWin32只能运行在Windows平台,所 ...

  10. 由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载,但是ajax实现的文件下载并不能触发浏览器的下载文件弹出框,这里通过模拟表单提交实现同样的效果。

    由于想要实现下载的文件可以进行选择,而不是通过<a>标签写死下载文件的参数,所以一直想要使用JFinal结合ajax实现文件下载(这样的话ajax可以传递不同的参数),但是ajax实现的文 ...