valid BST

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.



10/31

Solution 1:

use the inorder traversal with stack(no recursion) and returns false immediately after a node > its inorder predecessor;

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isValidBST(TreeNode root) {
        // solution 1: in order traversal
        if(root == null)    return true;
        
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode p = root, prev = null;
        while(p != null || !stack.empty()){
            while(p != null){
                stack.push(p);
                p = p.left;
            }
            TreeNode top = stack.pop();
            if(prev != null && prev.val >= top.val) return false;
            prev = top;
            p = top.right;
        }
        
        return true;
        
    }
}

时间是O(n): 扫描所有的node

空间是O(n)/O(1): 如果用Morris Traversal那么复杂度就是O(1)


Solution 2:

recursion: each recursion should return the range to check

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isValidBST(TreeNode root) {
        if(root == null)    return true;
        else    return isValidBSTRec(root, new int[2]);
        
    }
    
    private boolean isValidBSTRec(TreeNode root, int[] range){
        // note that root cannot be null
        int[] left = new int[2];
        int[] right = new int[2];
        
        range[0] = root.val;
        range[1] = root.val;
        if(root.left != null){
            if(!isValidBSTRec(root.left, left))     return false; // err2: should call the recursive form
            // update the range
            if(root.val <= left[1])      return false;
            range[0] = left[0];
        }
        
        if(root.right != null){
            if(!isValidBSTRec(root.right, right))      return false;
            // update the range
            if(root.val >= right[0])     return false; // err1: ask if it can be =?
            range[1] = right[1];
        }
        
        return true;
        
    }
}

The time complexity again is O(n): check all the nodes

The space complexity is O(n): maintain two range values for each node

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值