题目

纯暴力

 1 class Solution {
2 public:
3 vector<int>ans;
4 int findSecondMinimumValue(TreeNode* root) {
5 dfs(root);
6 sort(ans.begin(),ans.end());
7 int res = ans[0];
8 for(int i = 1;i < ans.size();i++){
9 if(ans[i] != res) return ans[i];
10 }
11 return -1;
12 }
13 void dfs(TreeNode* root){
14 if(root!=NULL){
15 dfs(root->left);
16 ans.push_back(root->val);
17 dfs(root->right);
18 }
19 }
20
21 };

递归

 1 class Solution {
2 public:
3
4 int findSecondMinimumValue(TreeNode* root) {
5 if(!root || !root->left || !root->right) return -1; //不满足题意
6 int left = root->left->val,right = root->right->val;
7 if(root->val == root->left->val) left = findSecondMinimumValue(root->left);
8 if(root->val == root->right->val) right = findSecondMinimumValue(root->right);
9
10 if(root->val == left && root->val == right ) return -1;
11 if(root->val < min(left,right)) return min(left,right);
12 else return max(left,right);
13 }
14
15 };

LeetCode671. 二叉树中第二小的节点的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  8. C#LeetCode刷题之#671-二叉树中第二小的节点(Second Minimum Node In a Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4100 访问. 给定一个非空特殊的二叉树,每个节点都是正数,并且每 ...

  9. [LeetCode] 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 ...

随机推荐

  1. vue第八单元(组件通信 子父,父子组件通信 自定义事件 事件修饰符 v-model props验证 )

    第八单元(组件通信 子父,父子组件通信 自定义事件 事件修饰符 v-model props验证 ) #课程目标 掌握使用props让父组件给子组件传参(重点) 掌握props属性的使用以及prop验证 ...

  2. Spring Boot 简单入门案例

    第一:打开idea 找到spring  Initializr 第二:点击Next 在点击下一步 找到web之后勾选第一个spring web 就完成了 在写一个类 点击运行 结果如下:

  3. RabbitMQException com.rabbitmq.client.ShutdownSignalException: connection error; protocol meth

    异常1 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ...

  4. 上传报错,ITMS-90167,解决办法

    ERROR ITMS-90167 No .app bundles found in the package 报这个错误的原因是上传工具的版本问题或者本地网络问题. 解决办法是使用在线最新的上传工具,推 ...

  5. jrebel 启动失败的处理

    jrebel 启动失败的处理 今天使用 jrebel 启动项目的时候,突然啥日志都没有,只有一句Disconnected from the target VM, address: '127.0.0.1 ...

  6. Spring Boot Starters

    Spring Boot Starters 摘自 https://www.nosuchfield.com/2017/10/15/Spring-Boot-Starters/ 2017-10-15 Spri ...

  7. 二进制格式mysql

    1.二进制MySQL安装 #下载二进制格式的mysql软件包 wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31- ...

  8. Spring—SSJ集成&声明式事务管理

    1.   课程介绍 1.  SSJ集成;(掌握) 2.  声明式事务管理;(掌握) 什么是三大框架 2.1.  ssh Struts/Struts2 Spring Hibernate 2.2.  ss ...

  9. 基于jupyter lab搭建网页编程环境并添加自定义python kernel和matlab kernel以及plotly的使用

    内容转载自我的博客 目录 说明 1. 创建虚拟环境jupyter 2. 安装nodejs(用于jupyterlab安装扩展) 3. 安装pip包 4. 使用jupyterlab 5. 配置jupyte ...

  10. vue中Echarts的使用-自选效果

    由于项目要求使用数据图,于是我选择了我们的Echarts用来实现效果 一:全局安装Echarts npm install echarts --save(这个安装的是最新的版本有时候回报init未定义) ...