[Leetcode]98.validate Binary Search Tree

Source: Internet
Author: User

"title"

Given a binary tree, determine if it is a valid binary search tree (BST).

Assume a BST is defined as follows:

    • The left subtree of a node contains only nodes with keys less than the node ' s key.
    • The right subtree of a node contains only nodes with keys greater than the node ' s key.
    • Both the left and right subtrees must also is binary search trees.

Confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.


"Analysis"

No

"Code"

/********************************** Date: 2014-12-27* sjf0115* title: 98.Validate Binary Search tree* Source: Https://oj.leet code.com/problems/validate-binary-search-tree/* results: ac* Source: leetcode* Summary: **********************************/#    Include <iostream> #include <climits>using namespace std;struct TreeNode {int val;    TreeNode *left;    TreeNode *right;        TreeNode (int x): Val (x), left (null), right (NULL) {}};class solution {Public:bool isvalidbst (TreeNode *root) {        if (root = NULL) {return true;    }//if return Isvalidbst (Root,int_min,int_max,false,false);            }private:bool Isvalidbst (treenode* node,long min,long max,bool validmin,bool validmax) {if (node = = NULL) {        return true; }//min Max first do not use//root node is greater than left subtree all nodes are smaller than right subtree all nodes if (Validmax && node->val >= max) | | (Validmin && node->val <= min))        {return false; }//Zuozi whether to satisfy bool left = ISvalidbst (node->left,min,node->val,validmin,true);        Whether the right subtree satisfies bool Isvalidbst (NODE-&GT;RIGHT,NODE-&GT;VAL,MAX,TRUE,VALIDMAX);    return left && right;    }//};//creates a two-fork-tree int createbtree (treenode*& T) {int data in order sequence;    Enter the value of the node in the binary tree in order, 1 means empty tree cin>>data;    if (data = =-1) {T = NULL;        } else{T = new TreeNode (data);        Constructs left subtree Createbtree (t->left);    Construct right subtree Createbtree (t->right); } return 0;}    int main () {solution solution;    treenode* root = NULL;    Createbtree (root); Cout<<solution.isvalidbst (root) <<endl;}




"Wrong Solution"

Class Solution {public:    bool Isvalidbst (TreeNode *root) {        if (root = NULL) {            return true;        } If        //left dial hand tree        if (root->left) {            if (root->left->val >= root->val) {                return false;            } If        }//if        //Right subtree        if (root->right) {            if (root->right->val <= root->val) {                return false;            } If        }//if        bool left = ISVALIDBST (root->left);        bool right = ISVALIDBST (root->right);        return left && right;}    };


10

/   \

5 15

/   \

6 20

The algorithm considers only one node of the right node to a left node of the comparison, forgetting that the left Dial hand node is smaller than the parent node, less than the parent node ...

The right child node is greater than the parent node, which is greater than the parent node ...


"Wrong solution two"

Class Solution {public:    bool Isvalidbst (TreeNode *root) {        if (root = NULL) {            return true;        } If        return Isvalidbst (Root,int_min,int_max);    } Private:    bool Isvalidbst (treenode* node,int min,int max) {        if (node = = NULL) {            return true;        }        if (node->val >= max | | node->val <= min) {            return false;        } If        bool left = ISVALIDBST (node->left,min,node->val);        bool right = ISVALIDBST (Node->right,node->val,max);        return left && right;    } //};



Problem occurs if the node value equals int boundary value






[Leetcode]98.validate Binary Search 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.