Thought is actually very simple, but why I just can't think of it?????!!!!!
Recursive judgment, recursive when the passing of two parameters, one is the left, one is the right, the value of the node must be in the middle of two boundaries, while the decision to do the subtree and the right sub-tree to update the left and next bounds.
Need to consider the case of Int_max or int_min,
The corresponding changes are made to long long and Long_long_max and Long_long_min are passed after submission.
/** * Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * TreeNode (int x): Val (x), left (NULL) , right (NULL) {}}; */class Solution {public: bool Check (TreeNode *node, Long long leftval, long long rightval) { if (node = = NULL) return true; Return Leftval < Node->val && Node->val < rightval && Check (node->left, Leftval, node-> val) && Check (node->right, Node->val, rightval); } BOOL Isvalidbst (TreeNode *root) { //Start typing your/C + + solution below //do not write int main () function< C15/>return check (root, long_long_min, long_long_max); };
Validate binary Search tree--embodies the idea of two-searched tree