Leetcode_validate Binary Search Tree

Source: Internet
Author: User

I. TopicsValidate Binary Search TreeTotal Accepted: 51751 Total Submissions: 251219 My Submissions

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.

Show TagsHas you met this question in a real interview? Yes No

Discuss











two. Problem solving skillsThis problem is to determine whether a binary search tree is legal, this can be based on the definition of binary search tree to judge, that is, the value of an internal node is greater than the maximum value of the left subtree, at the same time less than the maximum value of the right subtree, according to this definition, can be recursive to judge, this method has a time complexity O ), the space complexity is O (logn). The point to note is to remember to update the maximum and minimum values of the tree with nodes as the parent node so that the previous call is judged when the recursion returns.

Three. Implementing the Code
#include <iostream>/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;*    TreeNode *right;* TreeNode (int x): Val (x), left (null), right (null) {}*};*/struct treenode{int val;    TreeNode *left;    TreeNode *right; TreeNode (int x): Val (x), left (null), right (null) {}};class Solution{private:bool isvalidbst (treenode* root, int &        MinValue, int &maxvalue) {if (!root) {return true;                } if (Root->left) {if (Root->val <= root->left->val) {            return false;            } int leftminvalue = 0;            int leftmaxvalue = 0;            if (!isvalidbst (Root->left, Leftminvalue, Leftmaxvalue)) {return false;                } else {MinValue = Leftminvalue; if (leftmaxvalue! = Leftminvalue) {if (root->val <= Leftmaxvalue) {return false;        }}}} else {MinValue = root->val;                } if (Root->right) {if (Root->val >= root->right->val) {            return false;            } int rightminvalue = 0;            int rightmaxvalue = 0;            if (!isvalidbst (Root->right, Rightminvalue, Rightmaxvalue)) {return false;                } else {MaxValue = Rightmaxvalue;                    if (rightmaxvalue! = Rightminvalue) {if (Root->val >= rightminvalue)                    {return false;        }}}} else {MaxValue = root->val;    } return true;    }public:bool Isvalidbst (treenode* root){int MinValue = 0;        int MaxValue = 0;        bool IsLeaf = true;    return Isvalidbst (Root, MinValue, MaxValue); }};






Four. ExperienceThis problem is mainly a two-fork search tree traversal, at the same time, to return the maximum and minimum subtree, in order to interpret the value of the node is legitimate, but also remember to update the node as the parent node of the maximum and minimum value of the subtree, so that recursive return when the last call to judge. This problem is not difficult, just pay attention to the details.


Copyright, welcome reprint, reproduced Please indicate the source, thank you




Leetcode_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.