/**
* 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. 高级测试岗位面试题---MARK

    直接手写一个python类 直接手写一个构造函数 紧接着上面的代码,直接手写,补充完整代码,要求对列表中的人进行排序,并筛选出分数大于80的人的名单,组成一个新的列表显示出来. class Perso ...

  2. 什么是QPS,PV

    术语说明: QPS = req/sec = 请求数/秒 [QPS计算PV和机器的方式] QPS统计方式 [一般使用 http_load 进行统计] QPS = 总请求数 / ( 进程总数 * 请求时间 ...

  3. Python: Tkinter、ttk编程之计算器

    起源: 研究Python UI编程,我偏喜欢其原生组件,于是学习Tkinter.ttk组件用法.找一计算器开源代码,略加修整,以为备忘.其界面如图所示: 1.源代码(Python 2.7): # en ...

  4. java 线程Thread 技术--方法演示生产与消费模式

    利用wait 与notifyAll 方法进行演示生产与消费的模式的演示,我们两个线程负责生产,两个线程消费,只有生产了才能消费: 在effective Java 中有说过: 1. 在Java 中 ,使 ...

  5. 序列化_Transient

    要实际的操作一下Serialize的代码Demo, 加深理解(某投行很喜欢问这个问题):transient关键字虽然目前还没有人面试过我,但是也是个考点

  6. Java(JFinal)实现sqlserver2017的数据库的备份与恢复

    1.连接数据库的代码: package com.once.xfd.dbutil; import java.sql.Connection; import java.sql.DriverManager; ...

  7. BCHABC/BCHSV的矛盾所在

    BCHABC: 将BCH以后发展智能合约: 消息方面: 吴忌寒:BCH分叉不可避免 未来可能继续分叉 近日,吴忌寒在北大光华管理学院的区块链培训课程上发表演讲表示,在这种言论自由地环境下,我认为分裂就 ...

  8. 9.4-9.19 h5日记总结

    总结: 1.标签 (1)单.双标签 (2)块级.行级标签 (3)标签的属性 2.CSS (1)选择器 *.id.class.标签.后代.子代.并集.交集.伪类.结构 (2)层叠性,即选择器权重的计算 ...

  9. RoCE vs iWARP

    两种以太网 RDMA 协议: iWARP 和 RoCE 转载 2017年03月08日 16:10:09 1510 http://weibo.com/p/1001603936363903889917?m ...

  10. N! (数组)

    #include <iostream> using namespace std; ; int f[MAXN]; int main(){ int n; cin >> n; f[] ...