【LeetCode-Medium Question】98. Verify Binary Search Tree

topic

insert image description here
insert image description here

The idea is to first know what is a binary search tree
The left child is in the interval of (the minimum value of the parent node, the parent node)
The right child is in the range of (parent node, maximum value of parent node)
As long as these two points are satisfied

Method 1: BFS layer order traversal

Use layer order traversal to get each node and equip each node with a queue with a maximum value and a minimum value
As long as the node is between the maximum value and the minimum value, the condition of the binary search tree is satisfied
insert image description here

insert image description here

public boolean isValidBST(TreeNode root) {
    
    

        if(root == null) return true;

        Queue<TreeNode> queue = new LinkedList<>();
        Queue<Long> minValues = new LinkedList<>();//定义两个队列来记录每一个节点的最大值和最小值情况  
        Queue<Long> maxValues = new LinkedList<>();

        queue.offer(root);
        minValues.offer(Long.MIN_VALUE); // 初始最小值为
        maxValues.offer(Long.MAX_VALUE); // 初始最大值为
        while(!queue.isEmpty()){
    
    
            int count = queue.size();
            for(int i = 0 ; i < count ; i++){
    
    
                TreeNode node =  queue.poll();
                Long minValue = minValues.poll();//弹出该对比节点的最大值和最小值情况   节点值必须在这个区间内才满足条件
                Long maxValue = maxValues.poll();
                if (  node.val <= minValue ||  node.val >= maxValue) {
    
    
                    return false;
                }
                if(node.left != null){
    
    
                    queue.offer(node.left);
                    minValues.offer(minValue);//  左子树的最小值沿用上一次的最小值
                    maxValues.offer((long)node.val); // 左子树的最大值为当前节点值
                }
                if(node.right != null){
    
    
                    queue.offer(node.right);
                    minValues.offer((long)node.val); // 右子树的最小值为当前节点值
                    maxValues.offer(maxValue); // 右子树的最大值沿用上一次的最大值
                }
            }
    }
    return true;

    }

Method 2: Recursive

	   // root.val要在  (min,max) 区间才是二叉搜索数
      //  判断左子树 和右子树是否是搜索二叉树   
     //   ==左孩子在(父节点的最小值,父节点)区间内==
	// 	  ==右孩子在(父节点,父节点的最大值)区间内==
    public boolean isValidBST(TreeNode root) {
    
    
            return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);   // 不能用Integer.MAX  2147483647  案例有root就等于2147483647  明显不满足搜索树
    }
     public boolean isValidBST(TreeNode root,long  min,long  max) {
    
    
           if(root == null ){
    
    
               return true;
           }
           if(root.val <= min || root.val >= max) return false;//root.val要在  (min,max) 区间才是二叉搜索数
        
           return isValidBST(root.left,min,root.val)&&isValidBST(root.right,root.val,max);
           //判断左子树 和右子树是否是搜索二叉树   
         //   ==左孩子在(父节点的最小值,父节点)区间内==
		// 	  ==右孩子在(父节点,父节点的最大值)区间内==
 
    }

Method 3: Inorder traversal (stack)

  1. The core first traverses the left subtree until the left subtree is null and then traverses the right subtree until the right subtree is null
  2. The value of each pop-up node is less than or equal to the previous inorder, indicating that it is not a binary search tree
  3. Update the inorder value to the current node while traversing the right subtree
  public boolean isValidBST(TreeNode root) {
    
    
         Deque<TreeNode> stack = new LinkedList<TreeNode>();//栈
          Long inorder = Long.MIN_VALUE;

          while(!stack.isEmpty() || root !=null){
    
    
              while(root != null){
    
    //先去遍历左子树
                stack.push(root);
                root = root.left;
              }

              root = stack.pop();
               // 如果中序遍历得到的节点的值小于等于前一个 inorder,说明不是二叉搜索树
              if(root.val <= inorder) return false;

                inorder = (long)root.val;
                root = root.right;//遍历右子树

          }
     return true;
    }

Method 4: Inorder traversal (recursion)

During the in-order traversal, judge whether the current node is greater than the previous node in the in-order traversal. If it is greater, it means that the BST is satisfied and the traversal continues; otherwise, return false directly.

 long pre = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
    
    
       
        if(root == null) return true;
        //判断左子树是不是 二叉搜索时
        if(!isValidBST(root.left)) return false;

        if(root.val <= pre) return false ;
        else pre = root.val;

         //判断右子树是不是 二叉搜索时
        if(!isValidBST(root.right)) return false;

        return true;
    }

Guess you like

Origin blog.csdn.net/weixin_45618869/article/details/132570788