zoukankan      html  css  js  c++  java
  • leetcode--98. Validate Binary Search Tree

    1、问题描述

    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

    Binary tree [2,1,3], return true.

    Example 2:

        1
       / 
      2   3

    Binary tree [1,2,3], return false.

    2、边界条件:root==null?

    3、思路:1)二叉搜索数的性质,中序遍历就是一个升序排列方式。利用这个性质,先序遍历树得到一个数组,然后判断数组是否为升序排列。可以在最终数组判断,也可以遍历一个子树就判断。

    2)一个节点的值就限制了它的左子树的最大值,右子树的最小值。这样节点的信息就可以向下传递。

    4、代码实现

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isValidBST(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            inOrder(result, root);
            if (result.size() <= 1) {
                return true;
            }
            for (int i = 0; i < result.size() - 1; i++) {
                if (result.get(i) >= result.get(i + 1)) {
                    return false;
                }
            }
            return true;
        }
        public void inOrder(List<Integer> result, TreeNode root) {
            if (root == null) {
                return;
            }
            inOrder(result, root.left);
            result.add(root.val);
            inOrder(result, root.right);
        }
    }

    错误做法:对于一个节点,判断val是否大于left,小于right;然后判断子树是否为BST。这样判断只是确定了3个值的关系,并没有确定节点和子树的关系。[10,5,15,null,null,6,20]不通过。

    class Solution {
        public boolean isValidBST(TreeNode root) {
            if (root == null) {
                return true;
            }
            if (root.left != null && root.left.val >= root.val) {
                return false;
            }
            if (root.right != null && root.right.val <= root.val) {
                return false; 
            }
            return isValidBST(root.left) && isValidBST(root.right);
        }
    }

     在递归过程中判断每个子树是否为BST,这种方式也能实现,但是时间复杂度不能保证,大部分情况比较慢。

    class Solution {
        public boolean isValidBST(TreeNode root) {
            List<Integer> result = new ArrayList<Integer>();
            return inOrder(result, root);
        }
        public boolean inOrder(List<Integer> result, TreeNode root) {
            if (root == null) {
                return true;
            }
            if (!inOrder(result, root.left)) {
                return false;
            }
            result.add(root.val);
            if (!inOrder(result, root.right)) {
                return false;
            }
            if (result.size() <= 1) {
                return true;
            }
            for (int i = 0; i < result.size() - 1; i++) {
                if (result.get(i) >= result.get(i + 1)) {
                    return false;
                }
            }
            return true;
        }
    }

    方法二---推荐

    class Solution {
        public boolean isValidBST(TreeNode root) {
            return isValidBST(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
        }
        public boolean isValidBST(TreeNode root, int minVal, int maxVal) {
            if (root == null) {
                return true;
            }
            if (root.val <= minVal || root.val >= maxVal) {
                return false;
            }
            return isValidBST(root.left, minVal, root.val)
                   && isValidBST(root.right, root.val, maxVal);
        }
    }

    5、时间复杂度:遍历一遍树O(N), 空间复杂度:O(N),数组

    6、api

    这里讨论了一种方法,可以看一下:https://discuss.leetcode.com/topic/4659/c-in-order-traversal-and-please-do-not-rely-on-buggy-int_max-int_min-solutions-any-more

  • 相关阅读:
    CVE-2020-14882&14883weblogic未授权命令执行漏洞复现
    不学点《近世代数》怎么学好现代密码学
    Android 7.0应用之间共享文件
    在.NET Core 中收集数据的几种方式
    浅析 TensorFlow Runtime 技术
    Linux — 进程管理
    【爬虫】爬取淮安信息职业学校(苏电院)的新闻网 python
    【爬虫】获取Github仓库提交纪录历史的脚本 python
    React入门学习笔记
    test
  • 原文地址:https://www.cnblogs.com/shihuvini/p/7461062.html
Copyright © 2011-2022 走看看