The test case changed and this is the update edition of this problem


  • 0
    H

    /**

    • Definition for binary tree
    • struct TreeNode {
    • int val; 
      
    • TreeNode *left; 
      
    • TreeNode *right; 
      
    • TreeNode(int x) : val(x), left(NULL), right(NULL) {} 
      
    • };
      /
      class Solution {
      public:
      int last = INT_MIN;
      bool first = true;
      bool dfs(TreeNode
      root)
      {
      if(root == NULL) return true;
      if(root ->left)
      if(!dfs(root->left))
      return false;
      if(!first)
      {
      if(root ->val <= last)
      return false;
      else
      last = root->val;
      }
      else
      {
      last = root->val;
      first = false;
      }
      if(root->right)
      if(!dfs(root->right))
      return false;
      return true;
      }
      bool isValidBST(TreeNode *root) {
      // Start typing your C/C++ solution below
      // DO NOT write int main() function
      return dfs(root);
      }
      }; enter code here

Log in to reply
 

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