C++ dfs solution, bottom-up


  • 0
    J
    class Solution {
    public:
        int findSecondMinimumValue(TreeNode* root) {
            return dfs(root,root->val);
        };
    private:
        int dfs(TreeNode *root,int first)
        {
            if(!root) return -1;
            if(root->val != first) return root->val;
            int left = dfs(root->left,first);
            int right = dfs(root->right,first);
            if(left == -1) return right;
            if(right == -1) return left;
            return min(left,right);
        }
    };
    

Log in to reply
 

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