码迷,mamicode.com
首页 > 其他好文 > 详细

CTCI 4.5

时间:2014-07-13 22:27:09      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   cti   io   div   

Implement a function to check if a binary tree is a binary search tree.

/* The inorder travel of a BST is strictly increasing. We track the pre node of each 
node during the recursive process, and compare the two node‘s value to determine whether 
the tree is BST*/

public class IsBinarySearchTree {
    boolean stat = true;
    public boolean isValidBST(TreeNode root) {
        inorder(root, null);
        return stat;
    }
    
    private TreeNode inorder(TreeNode node, TreeNode pre) {
        if(node == null) return pre;
        else {
            TreeNode temp = inorder(node.left, pre);
            if(temp != null && node != temp && node.val <= temp.val) stat = false;
            temp = inorder(node.right, node);
            return temp;
        }
    }
}

 

CTCI 4.5,布布扣,bubuko.com

CTCI 4.5

标签:style   blog   color   cti   io   div   

原文地址:http://www.cnblogs.com/pyemma/p/3840574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!