bool validTree(TreeNode *root, int minVal, int maxVal){ if (root == NULL){ return true; } if (root -> val <= minVal || root -> val >= maxVal){ return false; } return validTree(root -> left, minVal, root -> val) && validTree(root -> right, root -> val, maxVal); }
int main(){ vector<vector<int> > edges = { {0, 1}, {0, 2}, {0, 3}, {1, 4} }; cout<<validTree(5, edges)<<endl; return 0; }
bool isValidBST(TreeNode *root) { //每一颗二叉树检查的时候子树都有一个最大值和最小值,具体数值由递归树传递过来 return validTree(root, INT_MIN, INT_MAX); }