Grokking LinkedIn Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content

Second Minimum Node In a Binary Tree (easy)
Table of Contents

Problem Statement

Examples

Try it yourself

Problem Statement

You are given a non-empty special binary tree consisting of nodes with a positive value, where each node in this tree has exactly two or zero children. If the node has two children, then the current node's value is the smaller value among its two children. In other words, the property root.val = min(root.left.val, root.right.val) always holds for each node of the tree.

Given such a binary tree, return the second minimum value in the set made using all the nodes' values in the whole tree. If no such second minimum value exists, output -1 instead.

Examples

Example 1:

  • Input: [4, 4, 5, null, null, 5, 6]
Image
  • Expected Output: 5
  • Justification: The smallest value is 4, and the second smallest is 5.

Example 2:

  • Input: [3, 3, 4, null, null, 5, 4, 5, 6, null, null]
Image
  • Expected Output: 4
  • Justification: The smallest value is 3. The second smallest value, which is distinct from the smallest, is 4.

Example 3:

  • Input: [2, 2, 2, null, null, 2, 2]
Image
  • Expected Output: -1
  • Justification: All values in the tree are the same (2), so there is no second minimum value.

Try it yourself

Try solving this question here:

Python3
Python3

. . . .
Mark as Completed

Table of Contents

Problem Statement

Examples

Try it yourself