C++ program throwing runtime error


  • 0
    D
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
     #include <algorithm>
     using namespace std;
    class Solution {
    public:
     int maxValue(TreeNode *root) {
         if (root == NULL) return -1;
         int leftMax =  maxValue(root->left);
         int rightMax = maxValue(root->right);
         int subtreeMax =  max(leftMax,rightMax);
        return max(root->val, subtreeMax);
         
     }
     int minValue(TreeNode *root) {
          if (root == NULL) return -1;
         int leftMin =  minValue(root->left);
         int rightMin = minValue(root->right);
         int subtreeMin =  min(leftMin,rightMin);
        return min(root->val, subtreeMin);
     }
        bool isValidBST(TreeNode* root) {
            int maxValueOfLeftSubtree = maxValue(root->left);
            int minValueOfRightSubtree = minValue(root->right);
            if ((root->val > maxValueOfLeftSubtree) && 
            (root->val < minValueOfRightSubtree))
                return true;
            
            return false;
            
        }
    };

Log in to reply
 

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