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

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

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

给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1。

每日一算法
2019/5/12Day 9
LeetCode671. Second Minimum Node In a Binary Tree

示例 1:

输入:

    2
   /   2   5
     /     5   7

输出: 5
说明: 最小的值是 2,第二小的值是 5。

示例 2:

输入:

    2
   /   2   2

输出: -1
说明: 最小的值是 2,但是不存在第二小的值。

Java 实现
Recursive Solution

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

class Solution {
    public int findSecondMinimumValue(TreeNode root) {
        if (root == null) {
            return -1;
        }
        if (root.left == null && root.right == null) {
            return -1;
        }
        int left = root.left.val;
        int right = root.right.val;
        if (left == root.val) {
            left = findSecondMinimumValue(root.left);
        }
        if (right == root.val) {
            right = findSecondMinimumValue(root.right);
        }
        if (left != -1 && right != -1) {
            return Math.min(left, right);
        } else if (left != -1) {
            return left;
        } else {
            return right;
        }
    }
}

相似题目

  • 230. 二叉搜索树中第K小的元素

参考资料

原文地址:https://www.cnblogs.com/hglibin/p/10851924.html

时间: 2024-11-08 20:22:11

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

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

给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有节点中的第二小的值.如果第二小的值不存在的话,输出 -1 . 示例 1: 输入: 2 / \ 2 5 / \ 5 7 输出: 5说明: 最小的值是 2 ,第二小的值是 5 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/second-minimum-n

[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 node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.

【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 node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.

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 node in this tree has exactly twoor zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. G

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 node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes.

六:二叉树中第k层节点个数与二叉树叶子节点个数

二叉树中第k层节点个数 递归解法: (1)如果二叉树为空或者k<1返回0 (2)如果二叉树不为空并且k==1,返回1 (3)如果二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树k-1层节点个数之和 代码如下: int GetNodeNumKthLevel(BinaryTreeNode *pRoot, int k) { if(pRoot == NULL || k < 1) return 0; if(k == 1) return 1; int numLeft = GetNodeN

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree 链接 leetcode 描述 ??Given two binary trees original and cloned and given a reference to a node target in the original tree. ??The cloned tree is a copy of the original tr

LeetCode OJ - Minimum &amp;&amp; Maximum Depth of Binary Tree

这两道题用递归的解法都很简单,只是稍有不同. 下面是AC代码: 1 /** 2 * Given a binary tree, find its minimum depth. 3 * the minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 4 * @param root 5 * @return 6 */ 7 public in

图解leetcode —— 124. 二叉树中的最大路径和

前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: [1,2,3] 1 / \ 2 3 输出: 6 示例 2: 输入: [-10,9,20,null,null,15,7] -10   / \  9  20    /  \   15   7 输出: 42 思路: java