671. Second Minimum Node In a Binary Tree LeetCode Solution

In this guide, you will get 671. Second Minimum Node In a Binary Tree LeetCode Solution with the best time and space complexity. The solution to Second Minimum Node In a Binary Tree problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Second Minimum Node In a Binary Tree solution in C++
  4. Second Minimum Node In a Binary Tree solution in Java
  5. Second Minimum Node In a Binary Tree solution in Python
  6. Additional Resources
671. Second Minimum Node In a Binary Tree LeetCode Solution image

Problem Statement of 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. More formally, the property root.val = min(root.left.val, root.right.val) always holds.
Given such a binary tree, you need to output the second minimum value in the set made of all the nodes’ value in the whole tree.
If no such second minimum value exists, output -1 instead.

Example 1:

Input: root = [2,2,5,null,null,5,7]
Output: 5
Explanation: The smallest value is 2, the second smallest value is 5.

Example 2:

Input: root = [2,2,2]
Output: -1
Explanation: The smallest value is 2, but there isn’t any second smallest value.

Constraints:

The number of nodes in the tree is in the range [1, 25].
1 <= Node.val <= 231 – 1
root.val == min(root.left.val, root.right.val) for each internal node of the tree.

See also  820. Short Encoding of Words LeetCode Solution

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(h)

671. Second Minimum Node In a Binary Tree LeetCode Solution in C++

class Solution {
 public:
  int findSecondMinimumValue(TreeNode* root) {
    if (root == nullptr)
      return -1;
    return findSecondMinimumValue(root, root->val);
  }

 private:
  int findSecondMinimumValue(TreeNode* root, int mn) {
    if (root == nullptr)
      return -1;
    if (root->val > mn)
      return root->val;

    const int leftMin = findSecondMinimumValue(root->left, mn);
    const int rightMin = findSecondMinimumValue(root->right, mn);

    if (leftMin == -1 || rightMin == -1)
      return max(leftMin, rightMin);
    return min(leftMin, rightMin);
  }
};
/* code provided by PROGIEZ */

671. Second Minimum Node In a Binary Tree LeetCode Solution in Java

class Solution {
  public int findSecondMinimumValue(TreeNode root) {
    if (root == null)
      return -1;
    return findSecondMinimumValue(root, root.val);
  }

  private int findSecondMinimumValue(TreeNode root, int mn) {
    if (root == null)
      return -1;
    if (root.val > mn)
      return root.val;

    final int leftMin = findSecondMinimumValue(root.left, mn);
    final int rightMin = findSecondMinimumValue(root.right, mn);

    if (leftMin == -1 || rightMin == -1)
      return Math.max(leftMin, rightMin);
    return Math.min(leftMin, rightMin);
  }
}
// code provided by PROGIEZ

671. Second Minimum Node In a Binary Tree LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.