98. Validate Binary Search Tree

题目:

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.

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

题解:

一开始使用的是BST定义,结果遇到一些边界条件会出问题,比如 Integer.MAX_VALUE, #,Integer.MAX_VALUE一类的。所以最后还是使用了recursive的in-order traversal。 代码依然参考了discussion, 自己要勤练习。

Time Complexity - O(n), Space Complexity - O(1)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    TreeNode lastNode; 
    public boolean isValidBST(TreeNode root) {
        if(root == null)
            return true;
        if(!isValidBST(root.left))
            return false;
        if(lastNode != null && lastNode.val >= root.val)
            return false;
        lastNode = root;
        if(!isValidBST(root.right))
            return false;
        return true;
    }
}

 

另 - 今天去看了一部电影叫<The Martian>,很好看,讲的是马特戴蒙扮演的航天员在火星受困最后被成功救助的故事。即使孤身一人身处险境,也不能放弃希望, 所以要好好学习编程,为人类航空航天事业做贡献(假如还来得及的话)。

 

二刷:

和一刷一样的方法,先建立一个辅助节点,再用in-order traversal遍历整个树并且进行判断

Java:

Time Complexity - O(n), Space Complexity - O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private TreeNode lastNode;
    
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        if (!isValidBST(root.left)) {
            return false;
        }
        if (lastNode != null && lastNode.val >= root.val) {
            return false;
        }
        lastNode = root;
        if (!isValidBST(root.right)) {
            return false;
        }
        return true;
    }
}

 

三刷:

Java:

Time Complexity - O(n), Space Complexity - O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    TreeNode prev;
    
    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        if (!isValidBST(root.left)) return false;
        if (prev != null && prev.val >= root.val) return false;
        prev = root;
        if (!isValidBST(root.right)) return false;
        return true;
    }
}

 

 

Reference:

https://leetcode.com/discuss/37320/o-1-space-java-solution-morris-in-order-traversal

https://leetcode.com/discuss/45425/c-simple-recursive-solution

https://leetcode.com/discuss/39567/simple-java-recursion-solution

https://leetcode.com/discuss/22234/my-java-inorder-iteration-solution

https://leetcode.com/discuss/27913/accepted-java-solution

posted @ 2015-04-18 12:21  YRB  阅读(1096)  评论(0编辑  收藏  举报