算法-leetcode-树相关问题- 98. 验证二叉搜索树

34, 98. 验证二叉搜索树

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

思路1: 中序遍历有序

思路2: 从上到下迭代遍历

package com.shangguigu.dachang.algrithm.A09_tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : 不二
 * @date : 2022/4/24-下午7:00
 * @desc : 98. 验证二叉搜索树
 * https://leetcode-cn.com/problems/validate-binary-search-tree/
 **/
public class A99_isValidBST {

    public static void main(String[] args) {
        // int[] nums = {3, 4, 1, 7, 6, 5, 2, 0, 9, 8, 10};
        /*int[] nums = {1, 15, 14, 17, 7, 2, 12, 3, 9, 11};
        BSTree bsTree = new BSTree();
        for (int i = 0; i < nums.length; i++) {
            bsTree.addNode(new TreeNode(nums[i]));
        }*/

        BSTree bsTree = new BSTree();
        TreeNode tn1 = new TreeNode(5);
        TreeNode tn2 = new TreeNode(4);
        TreeNode tn3 = new TreeNode(6);
        TreeNode tn4 = new TreeNode(3);
        TreeNode tn5 = new TreeNode(7);
        tn1.left = tn2;
        tn1.right = tn3;
        tn3.left = tn4;
        tn3.right = tn5;
        bsTree.root = tn1;

        bsTree.inorderTraversal();
        System.out.println(isValidBST_v1(bsTree.root));
        System.out.println(isValidBST_v2(bsTree.root));
    }

    /**
     * 思路2:从顶部往下遍历, 然后后续遍历的时候,设置上边界和下边界
     */
    public static boolean isValidBST_v2(TreeNode root){
        if (root == null) {
            return true;
        }
        return validator(root, null, null);
    }

    /**
     * 这都怎么想出来了,太妙了
     * @param root
     * @param lowerBound
     * @param upperBound
     * @return
     */
    public static boolean validator(TreeNode root, Integer lowerBound, Integer upperBound) {
        if (root == null) {
            return true;
        }

        if (lowerBound != null && root.val <= lowerBound) {
            return false;
        }
        if (upperBound != null && root.val >= upperBound) {
            return false;
        }

        return validator(root.left, lowerBound, root.val) && validator(root.right, root.val, upperBound);
    }



    /**
     * 思路1:通过中序遍历,如果数据是有序的,那么就是二叉搜索树(二叉排序树)
     * 把中序遍历的数据记录下来
     * @param root
     * @return
     */
    public static boolean isValidBST_v1(TreeNode root) {
        if (root == null) {
            return true;
        }

        boolean isValid = true;
        List<Integer> lists = new ArrayList<>();
        inorderArr(root, lists);
        Integer lst = null;
        for (int i = 0; i < lists.size(); i++) {
            if (lst != null && lists.get(i) <= lst) {
                isValid = false;
            }
            lst = lists.get(i);
        }
        return isValid;
    }

    public static void inorderArr(TreeNode root, List<Integer> lists) {
        if (root == null) {
            return;
        }
        inorderArr(root.left, lists);
        lists.add(root.val);
        inorderArr(root.right, lists);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值