98、验证二叉搜索树

  1. 98、验证二叉搜索树
    1. 示例 1:
    2. 示例 2:
  • 题解
    1. 1、递归判断
    2. 2、迭代
    3. 3、迭代法中序遍历
  • 98、验证二叉搜索树

    给定一个二叉树,判断其是否是一个有效的二叉搜索树。

    假设一个二叉搜索树具有如下特征:

    • 节点的左子树只包含小于当前节点的数。
    • 节点的右子树只包含大于当前节点的数。
    • 所有左子树和右子树自身必须也是二叉搜索树。

    示例 1:

    输入:
        2
       / \
      1   3
    输出: true
    

    示例 2:

    输入:
        5
       / \
      1   4
         / \
        3   6
    输出: false
    解释: 输入为: [5,1,4,null,null,3,6]。
         根节点的值为 5 ,但是其右子节点值为 4 。
    

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

    题解

    需要记录整棵子树的上界或者下界。

    1、递归判断

    • 代码
    class Solution {
      public boolean helper(TreeNode node, Integer lower, Integer upper) {
        if (node == null) return true;
    
        int val = node.val;
        if (lower != null && val <= lower) return false;
        if (upper != null && val >= upper) return false;
    
        if (! helper(node.right, val, upper)) return false;
        if (! helper(node.left, lower, val)) return false;
        return true;
      }
    
      public boolean isValidBST(TreeNode root) {
        return helper(root, null, null);
      }
    }
    
    
    // 链接:https://leetcode-cn.com/problems/validate-binary-search-tree/solution/yan-zheng-er-cha-sou-suo-shu-by-leetcode/
    
    // C++
    class Solution {
    
    }
    

    2、迭代

    利用栈实现与递归一样的思路

    • 代码
    class Solution {
      LinkedList<TreeNode> stack = new LinkedList();
      LinkedList<Integer> uppers = new LinkedList(),
              lowers = new LinkedList();
      // 将当前子树及其上下界加入栈  
      public void update(TreeNode root, Integer lower, Integer upper) {
        stack.add(root);
        lowers.add(lower);
        uppers.add(upper);
      }
    
      public boolean isValidBST(TreeNode root) {
        Integer lower = null, upper = null, val;
        update(root, lower, upper);
    
        while (!stack.isEmpty()) {
          root = stack.poll();
          lower = lowers.poll();
          upper = uppers.poll();
    
          if (root == null) continue;
          // 获取当前结点的值
          val = root.val;
          // 下界非法
          if (lower != null && val <= lower) return false;
          // 上界非法
          if (upper != null && val >= upper) return false;
          // 处理左子树
          update(root.right, val, upper);
          // 处理右子树
          update(root.left, lower, val);
        }
        return true;
      }
    }
    
    
    // 链接:https://leetcode-cn.com/problems/validate-binary-search-tree/solution/yan-zheng-er-cha-sou-suo-shu-by-leetcode/
    

    3、迭代法中序遍历

    • 代码
    /**
     * 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) {
            Stack<TreeNode> stack = new Stack<>();
            // 防止数值溢出的问题
            int inorder = - Double.MAX_VALUE;
    
            while(!stack.isEmpty() || root != null) {
                while(root != null) {
                    stack.push(root);
                    root = root.left;
                }
    
                root = stack.pop();
                // 中序遍历的每一个值应该大于前一个值
                if (root.val <= inorder) {
                    return false;
                }
                inorder = root.val;
                root = root.rigth;
            }
            return true;
        }
    
    }
    

    转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 1056615746@qq.com

    💰

    Title:98、验证二叉搜索树

    Count:599

    Author:攀登

    Created At:2020-07-26, 00:19:44

    Updated At:2024-06-15, 15:52:32

    Url:http://jiafeimao-gjf.github.io/2020/07/26/98%E3%80%81%E9%AA%8C%E8%AF%81%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/

    Copyright: 'Attribution-non-commercial-shared in the same way 4.0' Reprint please keep the original link and author.

    ×

    Help us with donation