LeetCode 75 - 98. Verify Binary Search Tree

LeetCode 75 - 98. Verify Binary Search Tree

1. Description of the topic:

Given the root node root of a binary tree, determine whether it is a valid binary search tree.

A valid binary search tree is defined as follows:

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

Example 1:

Input: root = [2,1,3]
Output: true
Example 2:

image-20221013160413373

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

hint:

The number of nodes in the tree ranges from [1, 10^4]
-2^31 <= Node.val <= 2^31 - 1

Source: LeetCode
Link: https://leetcode.cn/problems/validate-binary-search-tree
The copyright belongs to Leetcode.com. For commercial reprints, please contact the official authorization, for non-commercial reprints, please indicate the source.

2. Thinking analysis:

  1. What thought does this question examine? What is your train of thought?

    My thoughts on this topic are nothing more than two, one uses recursion, and the other uses the data structure of the stack.

    It is relatively simple to write recursively, because it is a binary search tree, requiring all nodes in the left subtree to be smaller than the root node, and all nodes in the right subtree to be larger than the root node. So we need to compare the root node with its left and right nodes.

    So we can use a function that initially passes in the root node and the minimum and maximum values ​​of int64 as lower and upper. Then in the function, we judge whether the current node is greater than lower and smaller than upper, if not, return false. Then we recurse into root.Left and root.Right. If it is root.Left, its lower remains unchanged, and its upper becomes the Val of its root node root. If it is root.Right, its upper remains unchanged, and its lower becomes the Val of the root node root.

  2. When doing the questions, did you pass it once, what problems did you encounter, and what details did you need to pay attention to?

    No, the problem was not properly modeled at first:

    return helper(root.Left,lower,root.Val) && helper(root.Right,root.Val,upper)
    

    There is no correct writing here. Through re-analysis, first compare the value of the left node with the lower node and the root node, and then compare the node of the right subtree with the value of the root node and upper node.

  3. There are several solutions, which solution has the lowest time complexity, which solution has the lowest space complexity, and what is the optimal solution? What are other people's solutions, and who is more efficient? If implemented in different languages, which language is the fastest?

    Binary search tree **Inorder traversal** The sequence of values ​​obtained must be in ascending order, which enlightens us to check in real time whether the value of the current node is greater than the value of the node traversed in the previous inorder traversal in real time during inorder traversal. Can.

    func isValidBST(root *TreeNode) bool {
          
          
        stack := []*TreeNode{
          
          }
        inorder := math.MinInt64
        for len(stack) > 0 || root != nil {
          
          
            for root != nil {
          
          
                stack = append(stack, root)
                root = root.Left
            }
            root = stack[len(stack)-1]
            stack = stack[:len(stack)-1]
            if root.Val <= inorder {
          
          
                return false
            }
            inorder = root.Val
            root = root.Right
        }
        return true
    }
    
    作者:LeetCode-Solution
    链接:https://leetcode.cn/problems/validate-binary-search-tree/solution/yan-zheng-er-cha-sou-suo-shu-by-leetcode-solution/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
    

3. AC code:

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func isValidBST(root *TreeNode) bool {
     
     
    return helper(root,math.MinInt64,math.MaxInt64)
}

func helper(root *TreeNode,lower, upper int) bool {
     
     
    if root == nil {
     
     
        return true
    }

    if root.Val >= upper || root.Val <= lower{
     
     
        return false
    }

    return helper(root.Left,lower,root.Val) && helper(root.Right,root.Val,upper)
} 

4. Summary:

The time complexity and space complexity of recursion are both O(n), and the space complexity and time complexity of in-order traversal are both O(n) using the stack.

Guess you like

Origin blog.csdn.net/qq_36045898/article/details/131589822