[C++] Solution


  • 0
    class Solution {
    public:
        int findSecondMinimumValue(TreeNode* root) {
            int m2 = -1;
            if (root) preorder(root, root->val, m2);
            return m2;
        }
    
    private:
        void preorder(TreeNode* node, const int m1, int& m2) {
            if (node->val != m1 && (m2 == -1 || node->val < m2))
                m2 = node->val;
            if (node->left) preorder(node->left, m1, m2);
            if (node->right) preorder(node->right, m1, m2);
        }
    };
    

Log in to reply
 

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