LeetCode 题解工作台

二叉树中第二小的节点

给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0 。如果一个节点有两个子节点的话,那么该节点的值等于两个子节点中较小的一个。 更正式地说,即 root.val = min(root.left.val, root.right.val) 总成立。 给出这样的一个二叉…

category

3

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · 二分·树·traversal

bolt

答案摘要

直接 DFS 遍历二叉树,找到大于 `root.val` 的最小值。若不存在,则返回 -1。 时间复杂度 ,空间复杂度 。其中 是二叉树的节点数。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 二分·树·traversal 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么该节点的值等于两个子节点中较小的一个。

更正式地说,即 root.val = min(root.left.val, root.right.val) 总成立。

给出这样的一个二叉树,你需要输出所有节点中的 第二小的值

如果第二小的值不存在的话,输出 -1

 

示例 1:

输入:root = [2,2,5,null,null,5,7]
输出:5
解释:最小的值是 2 ,第二小的值是 5 。

示例 2:

输入:root = [2,2,2]
输出:-1
解释:最小的值是 2, 但是不存在第二小的值。

 

提示:

  • 树中节点数目在范围 [1, 25]
  • 1 <= Node.val <= 231 - 1
  • 对于树中每个节点 root.val == min(root.left.val, root.right.val)
lightbulb

解题思路

方法一:DFS

直接 DFS 遍历二叉树,找到大于 root.val 的最小值。若不存在,则返回 -1。

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n)。其中 nn 是二叉树的节点数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findSecondMinimumValue(self, root: Optional[TreeNode]) -> int:
        def dfs(root):
            if root:
                dfs(root.left)
                dfs(root.right)
                nonlocal ans, v
                if root.val > v:
                    ans = root.val if ans == -1 else min(ans, root.val)

        ans, v = -1, root.val
        dfs(root)
        return ans
speed

复杂度分析

指标
时间and space complexity are dependent on the traversal method. In the worst case, the solution requires O(N) time to traverse all nodes, where N is the number of nodes in the tree. Space complexity depends on the depth of the tree, O(H) where H is the tree's height for DFS.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Does the candidate demonstrate an understanding of binary tree properties and DFS?

  • question_mark

    How efficiently does the candidate optimize the traversal process?

  • question_mark

    Can the candidate handle edge cases like trees without a second minimum value?

warning

常见陷阱

外企场景
  • error

    Incorrectly assuming that there will always be a second minimum value in the tree.

  • error

    Failure to correctly update the second minimum value during DFS traversal.

  • error

    Not properly handling trees with repetitive values, which may lead to incorrect results.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Modify the problem to work on a generic binary tree, where nodes can have any number of children.

  • arrow_right_alt

    Expand the problem to return the k-th smallest value instead of just the second minimum.

  • arrow_right_alt

    Adapt the problem to handle non-unique nodes or nodes with different values in a more complex tree structure.

help

常见问题

外企场景

二叉树中第二小的节点题解:二分·树·traversal | LeetCode #671 简单