← Home

671. Second Minimum Node In a Binary Tree

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
two pointersC++Markdown
671

The trick here is to name the state correctly, then let the implementation follow. For 671. Second Minimum Node In a Binary Tree, the solution in this repository is mainly a two pointers solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: two pointers, stack, sliding window.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are findSecondMinimumValue.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 4 ms, faster than 56.08% of C++ online submissions for Second Minimum Node In a Binary Tree.
02//Memory Usage: 8.6 MB, less than 64.29% of C++ online submissions for Second Minimum Node In a Binary Tree.
03
04/**
05 * Definition for a binary tree node.
06 * struct TreeNode {
07 *     int val;
08 *     TreeNode *left;
09 *     TreeNode *right;
10 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
11 * };
12 */
13class Solution {
14public:
15    int findSecondMinimumValue(TreeNode* root) {
16        stack<TreeNode*> stk;
17        TreeNode* cur;
18        long min1 = LONG_MAX, min2 = LONG_MAX;
19        
20        stk.push(root);
21        while(!stk.empty()){
22            cur = stk.top(); stk.pop();
23            if(cur->val < min1){
24                min2 = min1;
25                min1 = cur->val;
26            }else if(cur->val > min1 && cur->val < min2){
27                min2 = cur->val;
28            }
29            
30            if(cur->left) stk.push(cur->left);
31            if(cur->right) stk.push(cur->right);
32        }
33        return (min1 == min2) ? -1 : min2;
34    }
35};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.