Difference between binary tree and binary search tree

后端 未结 12 1655
情深已故
情深已故 2020-11-29 14:44

Can anyone please explain the difference between binary tree and binary search tree with an example?

12条回答
  •  萌比男神i
    2020-11-29 15:15

    As everybody above has explained about the difference between binary tree and binary search tree, i am just adding how to test whether the given binary tree is binary search tree.

    boolean b = new Sample().isBinarySearchTree(n1, Integer.MIN_VALUE, Integer.MAX_VALUE);
    .......
    .......
    .......
    public boolean isBinarySearchTree(TreeNode node, int min, int max)
    {
    
        if(node == null)
        {
            return true;
        }
    
        boolean left = isBinarySearchTree(node.getLeft(), min, node.getValue());
        boolean right = isBinarySearchTree(node.getRight(), node.getValue(), max);
    
        return left && right && (node.getValue()=min);
    
    }
    

    Hope it will help you. Sorry if i am diverting from the topic as i felt it's worth mentioning this here.

提交回复
热议问题