[LeetCode] C++: Intermediate Problem-Tree 98. Verify Binary Search Tree

98. Verify Binary Search Tree

Intermediate difficulty 914

Given a binary tree, judge whether it is a valid binary search tree.

Suppose a binary search tree has the following characteristics:

  • The left subtree of the node only contains numbers less than the current node.
  • The right subtree of the node only contains numbers greater than the current node.
  • All left subtrees and right subtrees themselves must also be binary search trees.

Example 1:

Input: 
    2 
   / \ 
  1 3 Output: true

Example 2:

Input:
     5 
   / \ 
  1 4 
     / \ 
    3 6 Output: false
 Explanation: The input is: [5,1,4,null,null,3,6]. 
     The value of the root node is 5, but the value of its right child node is 4.

1. In-order traversal

The sequence obtained after traversing the binary search tree in order must be in ascending order

Therefore, each time compare whether the current node value is greater than the node value obtained by the previous in-order traversal, if it is greater than that, it means that it is not a binary search tree

Note that the initial value of inorder should be set to a very small number, and after each node is traversed, it needs to be updated for inorder, so that every time the comparison is made, the previous node value is compared with the current node value, and it must be in order It can be traversed.

Note: Since the topic does not specify the size of the node value, it is best to set it as large as possible. Both of the following writing methods are possible.

        // long long inorder = (long long) INT_MIN - 1;

        long long inorder = LONG_MIN;

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        stack<TreeNode*> stk;
        long long inorder = (long long) INT_MIN - 1;

        while(root != nullptr || !stk.empty()){
            while(root != nullptr){
                stk.push(root);
                root = root->left;
            }
            root = stk.top();
            stk.pop();
            if(root->val <= inorder){
                return false;
            }
            inorder = root->val;
            root = root->right;
        }
        return true;
    }
};

2. Recursion

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool helper(TreeNode* root, long long low, long long high){
        if(root == nullptr){
            return true;
        }
        if(root->val <= low || root->val >= high){
            return false;
        }
        return helper(root->left, low, root->val) && helper(root->right, root->val, high);
    }
    bool isValidBST(TreeNode* root) {
        return helper(root, LONG_MIN, LONG_MAX);
    }
};

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/113694578