首页 > 其他 > 详细

CTCI 4.5

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!