【LeetCode】98. Validate Binary Search Tree

Difficulty:Medium

 More:【目录】LeetCode Java实现

Description

https://leetcode.com/problems/validate-binary-search-tree/

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.

Example 1:

    2
   /   1   3

Input: [2,1,3]
Output: true

Example 2:

    5
   /   1   4
     /     3   6

Input: [5,1,4,null,null,3,6]
Output: false
Explanation: The root node‘s value is 5 but its right child‘s value is 4.

Intuition

1.Method 1:

  * Determine if left subtree and right subtree are valid binary search trees

  * Compare root.val with maxValue in left subtree and minValue in right subtree;

2.Method 2:

  * give the maxium range {max, min} for root.val

  * update the {max, min} for subtree‘s root.

Solution

Method 1

    public boolean isValidBST(TreeNode root) {
        if(root==null )
            return true;
        TreeNode left = root.left, right=root.right;
        if(!isValidBST(left) || !isValidBST(right))
            return false;

        // find the maxNode in left subtree
        if(left!=null)
            while(left.right!=null)
                left=left.right;

        // find the minNode in right subtree
        if(right!=null)
            while(right.left!=null)
                right=right.left;

        return (left==null || left.val<root.val)
            &&(right==null || right.val>root.val);
    }

  

Method 2

    public boolean isValidBST(TreeNode root) {
        return helper(root, (long)Integer.MIN_VALUE-1, (long)Integer.MAX_VALUE+1);
    }

    private boolean helper(TreeNode node, long min, long max){
        if(node == null)
            return true;
        return (node.val < max && node.val>min)
            && helper(node.left, min, node.val)
            && helper(node.right, node.val, max);
    }

  

  

  

Complexity

Method 1:

Time complexity : O(nlogn)

Space complexity : O(n)

Method 2:

Time complexity : O(n)

Space complexity : O(n)

What I‘ve learned

 More:【目录】LeetCode Java实现

原文地址:https://www.cnblogs.com/yongh/p/11750691.html

时间: 2024-10-06 00:28:34

【LeetCode】98. Validate Binary Search Tree的相关文章

【LeetCode】98. Validate Binary Search Tree -判断是否为二叉排序树

一.描述: 二.思路: 二叉排序树(BST),中序遍历的结果一定是非递减序列(来自百度百科): 本题中对于BST的定义是要么大于,要么小与,即遍历结果只能是递增序列,故可以通过判断中序遍历的结果序列是否是递增序列,来判断是否为合法BST: 另一种方法是使用递归: 三.代码: 1.非递归,通过中序遍历结果判断: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left;

【一天一道LeetCode】#98. Validate Binary Search Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

[Lintcode]95. Validate Binary Search Tree/[Leetcode]98. Validate Binary Search Tree

95. Validate Binary Search Tree/98. Validate Binary Search Tree 本题难度: Easy Topic: Binary Tree Description 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 contain

leetcode 98 Validate Binary Search Tree ----- java

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

[leetcode]98. Validate Binary Search Tree验证BST

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

leetcode笔记:Validate Binary Search Tree

一. 题目描写叙述 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

[leedcode 98] Validate Binary Search Tree

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

98. Validate Binary Search Tree - Medium

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

【LeetCode】96 - Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 Tree Dynamic Programming Solution 1:  递归 1 class So