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 with keys greater than the node's key.
  • Both the left and right subtrees must also be binary search trees.

分类:二叉树

题意:判断二叉树是否为排序二叉树


解法1:递归判断,递归时传入两个参数,一个是左界,一个是右界,节点的值必须在两个界的中间,同时在判断做子树和右子树时更新左右界

/**
 * 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 validate(root, null, null);
	}
 
	public boolean validate(TreeNode root, TreeNode min, TreeNode max) {
	    if(root==null) return true;
		if(min!=null && root.val<=min.val) return false;
        if(max!=null && root.val>=max.val) return false;
        if(!validate(root.left, min, root)) return false;
        if(!validate(root.right, root, max)) return false;
        return true;
	}
}


解法2:中序遍历,然后判断是否从小到大排序

/**
 * 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) {
        List<Integer> arr = new ArrayList<Integer>();
        Inorder(root,arr);
        for(int i=1;i<arr.size();i++){
            if(arr.get(i-1)>=arr.get(i)) return false;
        }
        return true;
    }
    
    public void Inorder(TreeNode root,List<Integer> arr){
        if(root!=null){
            if(root.left!=null) Inorder(root.left,arr);
            arr.add(root.val);
            if(root.right!=null) Inorder(root.right,arr);
        }
    }
}

解法3:中序遍历,但是每次标记要访问节点的前一个节点,从而省去O(n)空间

public class Solution {
    // Keep the previous value in inorder traversal.
    TreeNode pre = null;
    
    public boolean isValidBST(TreeNode root) {
        // Traverse the tree in inorder.
        if (root != null) {
            // Inorder traversal: left first.
            if (!isValidBST(root.left)) return false;
            
            // Compare it with the previous value in inorder traversal.
            if (pre != null && root.val <= pre.val) return false;
            
            // Update the previous value.
            pre = root;
            
            // Inorder traversal: right last.
            return isValidBST(root.right);
        }
        return true;
     }
}

解法3的另外一种写法:

/**
 * 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) {
        if(Inorder(root)!=null) return false;
        return true;
    }

    TreeNode pre = null;
    public TreeNode Inorder(TreeNode root){
        if(root==null) return null;
        TreeNode left=Inorder(root.left);
        if(left!=null) return left;
        if(pre!=null&&pre.val>=root.val) return root;
        pre = root;
        TreeNode right=Inorder(root.right);
        if(right!=null) return right;
        return null;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值