LeetCode Validate Binary Search Tree

 // 64ms 过大的
1
/** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 bool reVal(int minLeft,TreeNode *root,int maxRight) 13 { 14 int val=root->val; 15 if(val<=minLeft||val>=maxRight) 16 return false; 17 bool leftre=true,rightre=true; 18 if(root->left) 19 leftre=reVal(minLeft,root->left,val); 20 if(leftre==false) 21 return false; 22 if(root->right) 23 rightre=reVal(val,root->right,maxRight); 24 if(rightre==false) 25 return false; 26 return true; 27 } 28 bool isValidBST(TreeNode *root) { 29 // Start typing your C/C++ solution below 30 // DO NOT write int main() function 31 if(root==NULL) 32 return true; 33 return reVal(-INT_MAX,root,INT_MAX); 34 } 35 };
原文地址:https://www.cnblogs.com/mengqingzhong/p/3116874.html