[LeetCode] 98. Validate Binary Search Tree, binarysearchtree
[Question]
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 be binary search trees.
Confused what"{1,#,2,3}"Means? > Read more on how binary tree is serialized on OJ.
[Analysis]
None
[Code]
/********************************** Date: * Author: SJF0115 * Subject: 98. validate Binary Search Tree * Source: https://oj.leetcode.com/problems/validate-binary-search-tree/* result: AC * Source: LeetCode * conclusion: * *********************************/# 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 ;} // do not use min max for the first time. // if (validMax & node-> val> = max) the root node is greater than all nodes in the left subtree and less than all nodes in the right subtree) | (validMin & node-> val <= min) {return false;} // determines whether the left subtree meets bool left = isValidBST (node-> left, min, node-> val, validMin, true); // whether the right subtree meets bool right = isValidBST (node-> right, node-> val, max, true, validMax ); return left & right ;}/// create a binary tree int CreateBTree (TreeNode * & T) in the first sequence {int data; // enter the value of the node in the binary tree in the first order.-1 indicates the empty tree cin> data; if (data =-1) {T = NULL ;} else {T = new TreeNode (data); // construct the left subtree CreateBTree (T-> left); // construct the right subtree CreateBTree (T-> right );} return 0;} int main () {Solution solution; TreeNode * root = NULL; CreateBTree (root); cout <solution. isValidBST (root) <endl ;}
[Troubleshooting]
Class Solution {public: bool isValidBST (TreeNode * root) {if (root = NULL) {return true;} // if // left subtree 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 bool left = isValidBST (root-> left); bool right = isValidBST (root-> right ); return left & right ;}};
10
/\
5 15
/\
6 20
This algorithm only takes into account the comparison of one root node, one right node, and one left node. It forgets that the left child node must be smaller than the parent node, and smaller than the parent node of the parent node ......
The right child node must be greater than the parent node and greater than the parent node of the parent node ......
[Solution 2]
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; }//};
If the node value is equal to the INT boundary value, a problem occurs.