98. Validate Binary Search Tree

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 nodes key.
The right subtree of a node contains only nodes with keys greater than the nodes key.
Both the left and right subtrees must also be binary search trees.
Example 1:
    2
   /   1   3
Binary tree [2,1,3], return true.
Example 2:
    1
   /   2   3
Binary tree [1,2,3], return false.

 

用的中序遍历, 分治法, 维护了一个全局变量

public boolean isValidBST(TreeNode root) {  
    ArrayList<Integer> pre = new ArrayList<Integer>();  
    pre.add(null);  
    return helper(root, pre);  
}  
private boolean helper(TreeNode root, ArrayList<Integer> pre)  
{  
    if(root == null)  
        return true;  
    boolean left = helper(root.left,pre);  
    if(pre.get(0)!=null && root.val<=pre.get(0))  
        return false;  
    pre.set(0,root.val);  
    return left && helper(root.right,pre);  
}  

第二种方法: 题意的转化: 根据题目中的定义来实现,其实就是对于每个结点保存左右界,也就是保证结点满足它的左子树的每个结点比当前结点值小,右子树的每个结点比当前结点值大

这个是改变的输入值, 其实也是先操作最小的节点值, 在将题意转化为内部操作, 再递归 , 只不过内部操作转化为了输入值.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isValidBST(TreeNode root) {
        return check(root, Long.MAX_VALUE, Long.MIN_VALUE);
    }
    private boolean check(TreeNode root, long max, long min) {
        if (root == null) return true;
        
        
        boolean left = check(root.left, root.val, min);
        boolean right = check(root.right, max, root.val);
        if (root.val >= max || root.val <= min) return false;
        
        return left && right;
    }
}

  

 考的是题意的转化即内部操作 

方法三: 将题意转化为类中的元素, 对类进行操作, 套路固定, 同108. Convert Sorted Array to Binary Search Tree

树节点常构造类, 把题意转化成类中元素, 操作类中的元素, 跟法二差不多, 

//改的是返回值类型, 不过题意也都放在返回值类型里面
class ResultType {
    boolean is_BST;
    int maxValue, minValue;
    ResultType(boolean is_BST, int maxValue, int minValue) {
        this.is_BST = is_BST;
        this.maxValue = maxValue;
        this.minValue = minValue;   
    }
}
public class Solution {
       public boolean isValidBST(TreeNode root) {
        // write your code here
        ResultType result = helper(root);
        return result.is_BST;
        
    }
    //改的是返回值类型, 不过题意也都放在返回值类型里面
    private ResultType helper(TreeNode root) {
        if (root == null) {
            
            // 当遇到null是, int 多赋值为Integer.MAX_VALUE,Integer.MIN_VALUE
            //具体是什么看 后面与null类型的节点的比较
            //此处为了后面的比较 与空节点比较时, 最大值等于根节点的最大值,最小值等于根节点的最小值.
            return new ResultType(true, Integer.MIN_VALUE, Integer.MAX_VALUE);
        }
        
        // divide
        ResultType left = helper(root.left);
        ResultType right = helper(root.right);
        
        //conquer: 从底往上回溯
        if (!left.is_BST || !right.is_BST || root.left != null && left.maxValue >= root.val 
        || right.minValue <= root.val && root.right !=null) { return new ResultType(false, Integer.MIN_VALUE, Integer.MAX_VALUE); } //题意的转化尽显在构造类中 return new ResultType(true, Math.max(root.val, right.maxValue), Math.min(root.val, left.minValue)); } }

  

 

以上是关于98. Validate Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章

98. Validate Binary Search Tree

LC.98.Validate Binary Search Tree

LeetCode98. Validate Binary Search Tree

98. Validate Binary Search Tree

98. Validate Binary Search Tree

98. Validate Binary Search Tree