[C++] Solution


  • 0
    class Solution {
    public:
        int findSecondMinimumValue(TreeNode* root) {
            if (!root) return -1;
            int res = INT_MAX;
            find(root, res);
            return res == INT_MAX ? -1 : res;
        }
    
    private:
        void find(TreeNode* n, int& res) {
            if (!n || !n->left) return;
            if (n->left->val == n->right->val) {
                find(n->left, res);
                find(n->right, res);
            }
            else if (n->left->val < n->right->val) {
                res = min(res, n->right->val);
                find(n->left, res);
            }
            else {
                res = min(res, n->left->val);
                find(n->right, res);
            }
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.