[LeetCode] 98. Validate Binary Search Tree

45 篇文章 0 订阅

原题链接:https://leetcode.com/problems/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:
在这里插入图片描述
Example 2:
在这里插入图片描述
Explanation: The root node’s value is 5 but its right child’s value is 4.
图中的右子节点4大于根节点5,该树不是二叉搜索树。

2. 解题思路

2.1 递归

本题最常见的方法就是递归。同时这个方法是LeetCode题解中给出的第一个方法。

在对于一个树的递归中,函数的参数是向下传递的,函数的返回值则是向上传递的。因此在设计递归函数的时候,不妨盯着这个树多看一看,想一想什么应该从根节点向下传递,什么应该从叶子节点向上传递。

在本题中,可以采用下面的递归方式
向上传递子树的true或者false,向下传递子树根节点应该有的范围。

于是,递归函数的返回值就应该是该树是否为二叉树的判断,而函数的参数就应该是根节点以及根节点的取值范围。进而完成代码即可。

需要注意的是,在设定初始范围的时候,max 和 min 是无限的,而不是int类型的最大值或者最小值。因为在测试样例中还真有val值是int类型最大值的节点。所以一开始设定初始范围都是用null来设定。

实现代码

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

2.2 迭代

能用递归完成的事情,一般迭代也可以做到,本题还有一种迭代的做法。使用堆栈保存根节点和范围。

实现代码

/**
 * 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) {
        if(root == null){
            return true; 
        }
        
        Stack<TreeNode> s = new Stack<>();
        Stack<Integer> Max = new Stack<>();
        Stack<Integer> Min = new Stack<>();
      
        s.push(root);
        Max.push(null);
        Min.push(null);
        
        TreeNode t = root;
        Integer low = null;
        Integer high = null;
        
        while(s.empty() == false){
            t = s.pop();
            low  = Min.pop();
            high = Max.pop();
            
            if (t != null){
                if(low != null && t.val <= low ){
                    return false;
                }
                if(high != null && t.val >= high ){
                    return false;
                }
                
                s.push(t.right);
                Min.push(t.val);
                Max.push(high);
                
                s.push(t.left);
                Min.push(low);
                Max.push(t.val);
            }
        }
        return true;
    }
}

2.3 中序遍历

中序遍历是指按照1 左子树 2 根节点 3 右子树的顺序来遍历一棵树。
如果用中序遍历一个二叉树,遍历的结果必定是递增的。因此可以利用这一点对一课树是不是二叉树进行判断。如果中序遍历结果不是递增的,则返回false。
实现代码

/**
 * 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) {
        if(root == null){
            return true; 
        }
        Stack<TreeNode> s = new Stack<>();
        Stack<Integer> ans = new Stack<>();
        //ans堆栈记录中序遍历结果,必须为递增,否则返回false
        
        while(s.empty() == false || root != null){
            //将所有左节点放入堆栈
            while(root != null){
                s.push(root);
                root = root.left;
            }
            //从堆栈中取出树最下端的左节点
            root = s.pop();
            if(ans.empty() == false && ans.peek() >= root.val){
                return false;
            }
            ans.push(root.val);
            root = root.right;
        }
        return true;
    }
}

3. 参考资料

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值