leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0671.cpp (655B)


      1 class Solution {
      2   public:
      3     int findSecondMinimumValue(TreeNode *root) {
      4         if (!root) return -1;
      5         int val = root->val;
      6         long long res = LONG_MAX;
      7         stack<TreeNode *> st;
      8         st.push(root);
      9         while (!st.empty()) {
     10             TreeNode *root = st.top();
     11             st.pop();
     12             if (!root->left) continue;
     13             int m = max(root->left->val, root->right->val);
     14             if (m != val) res = min(res, (long long)m);
     15             if (root->left->val == val) st.push(root->left);
     16             if (root->right->val == val) st.push(root->right);
     17         }
     18 
     19         return res != LONG_MAX ? res : -1;
     20     }
     21 };