↓Skip to main content
  1. LeetCode/

LeetCode 671: Second Minimum Node In a Binary Tree

·2 mins· ·
LeetCode Easy Tree Depth-First-Search Binary-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-29

  • Submissions: Wrong Answer ×4 → Accepted ×3 → Wrong Answer ×6 → Accepted ×3

Approach
#

Each node equals the smaller of its two children, so the root is the minimum. The second minimum is the smallest value larger than the root (Tree Template 1):

  • A node larger than the root: nothing smaller lies below it (children are never smaller), so return it.
  • A node equal to the root: the answer may lie below, so keep searching.

My Attempts
#

The early versions looked only at the root’s two children and took max(left, right), but the second minimum can be deeper: for [2, 2, 6, 3, 2] the left child 2 has a 3 below it, so the answer is 3; they returned 6. Later versions missed “all values equal” or “a single node”, returning inf or the root’s value instead of -1.

Solution
#

class Solution:
    def findSecondMinimumValue(self, root: Optional[TreeNode]) -> int:
        def find(node):
            if not node:
                return -1
            if node.val > root.val:
                return node.val
            l, r = find(node.left), find(node.right)
            if l == -1 or r == -1:
                return max(l, r)
            return min(l, r)
        return find(root)

Complexity
#

Time O(n), space O(h).

Mistakes to Avoid
#

  • The second minimum may be deep; do not look only at the root’s children.
  • If every value is the same, return -1.

Interview Explanation
#

The root is the minimum, so I look for the smallest value above it: a larger node returns immediately, nodes equal to the root keep searching, and I take the smaller valid result. Time O(n).

Related

LeetCode 637: Average of Levels in Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 637 note: level-order traversal, dividing each level’s sum by its count
LeetCode 563: Binary Tree Tilt
·1 min
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 563 note: return subtree sums in post-order; each node’s tilt is the difference of its subtree sums, added to the answer
LeetCode 572: Subtree of Another Tree
·2 mins
LeetCode Easy Tree Depth-First-Search String-Matching Binary-Tree
LeetCode 572 note: for every node of root, check whether its subtree equals subRoot; recurse with isSubtree, not by comparing only the children
LeetCode 501: Find Mode in Binary Search Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 501 note: a BST’s in-order is sorted, so equal values are contiguous; count the current run during in-order, resetting the answer on a new maximum and appending on a tie
LeetCode 543: Diameter of Binary Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 543 note: the diameter may skip the root; return heights in post-order while updating the answer with left + right height at every node, counting edges
LeetCode 404: Sum of Left Leaves
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 404 note: tell each child during DFS whether it is a left child, adding only left children that are leaves