leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0671.cpp (655B)
0 class Solution { 1 public: 2 int findSecondMinimumValue(TreeNode *root) { 3 if (!root) return -1; 4 int val = root->val; 5 long long res = LONG_MAX; 6 stack<TreeNode *> st; 7 st.push(root); 8 while (!st.empty()) { 9 TreeNode *root = st.top(); 10 st.pop(); 11 if (!root->left) continue; 12 int m = max(root->left->val, root->right->val); 13 if (m != val) res = min(res, (long long)m); 14 if (root->left->val == val) st.push(root->left); 15 if (root->right->val == val) st.push(root->right); 16 } 17 18 return res != LONG_MAX ? res : -1; 19 } 20 };