Validate binary Search tree--embodies the idea of two-searched tree

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.