【Hot100】LeetCode—98. 验证二叉搜索树


1- 思路

中序遍历(栈)+判断数组

  • 1- 中序遍历
    • 借助队列 Stackcur 指针实现
    • 1-1 Stack默认加入 root
    • 1-2 借助 while 实现遍历:
    • 1-3 如果 cur!=null 此时,继续左移
    • 1-4 否则,此时需要处理结果,将 cur.val 加到结果集,之后右移
  • 2- 将中序的结果存入数组
    • 遍历数组判断是否有序

2- 实现

⭐98. 验证二叉搜索树——题解思路

在这里插入图片描述

class Solution {
    public boolean isValidBST(TreeNode root) {
        if(root==null){
            return true;
        }
        inorder(root);
        int[] nums = new int[res.size()];
        for(int i = 0 ; i < res.size() ; i++){
            nums[i] = res.get(i);
        }
        for(int i = 1 ; i < nums.length;i++){
            if(nums[i-1] >= nums[i]){
                return false;
            }
        }
        return true;
    }
    

    List<Integer> res = new ArrayList<>();
    public List<Integer> inorder(TreeNode root){
        // 栈+队列

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        if(root==null){
            return res;
        }

        while(cur!=null || !stack.isEmpty()){
            if(cur!=null){
                stack.push(cur);
                cur = cur.left;
            }else{
                cur = stack.pop();
                res.add(cur.val);
                cur = cur.right;
            }
        }
        return res;
    }

}

3- ACM 实现

public class isValidBST {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public static TreeNode build(String str){
        if(str == null || str.length()==0){
            return null;
        }
        String input = str.replace("[","");
        input = input.replace("]","");
        String[] parts = input.split(",");

        Integer[] nums = new Integer[parts.length];
        for(int i = 0 ; i < parts.length;i++){
            if(!parts[i].equals("null")){
                nums[i] = Integer.parseInt(parts[i]);
            }else{
                nums[i] = null;
            }
        }
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);
        int index = 1;
        while(!queue.isEmpty()&& index<parts.length){
            TreeNode node = queue.poll();
            if(index<nums.length && nums[index]!=null){
                node.left = new TreeNode(nums[index]);
                queue.offer(node.left);
            }
            index++;
            if(index<nums.length && nums[index]!=null){
                node.right = new TreeNode(nums[index]);
                queue.offer(node.right);
            }
            index++;
        }
        return root;
    }

    public static boolean isBST(TreeNode root){
        inorder(root);
        // 构造数组
        int[] nums = new int[res.size()];
        for(int i = 0 ; i < nums.length;i++){
            nums[i] = res.get(i);
        }
        for(int i = 1 ; i < nums.length;i++){
            if(nums[i-1] >= nums[i]){
                return false;
            }
        }
        return true;
    }


    static List<Integer> res = new ArrayList<>();
    public static List<Integer> inorder(TreeNode root){
        // 栈
        Stack<TreeNode> st = new Stack<>();
        TreeNode cur = root;

        while(cur!=null || !st.isEmpty()){
            if(cur!=null){
                st.add(cur);
                cur = cur.left;
            }else{
                cur = st.pop();
                res.add(cur.val);
                cur = cur .right;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        TreeNode root = build(input);
        System.out.println("结果是"+isBST(root));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值