判断搜索二叉树

这篇博客介绍了四种方法来验证一个二叉树是否是二叉搜索树,包括两种递归和两种非递归的实现方式。核心思想是通过中序遍历检查节点值的升序顺序。递归方法基于中序遍历的性质,非递归方法则利用栈来模拟中序遍历。此外,还提出了一种新的递归策略,通过收集每个节点的信息(是否是BST,左子树的最大值,右子树的最小值)来判断整个树的合法性。
摘要由CSDN通过智能技术生成
import java.util.ArrayList;
import java.util.Stack;

public class testBST{
    public static class Node{
        public int value;
        public Node left;
        public Node right;

        public Node(int data){
            this.value = data;
        }
    }

    public static int preValue = Integer.MIN_VALUE;

    public static boolean checkBST(Node head){
        if(head == null){
            return true; 
        }
        boolean isLeftBst = checkBST(head.left);
        if(!isLeftBst){
            return false;//如果左树不是搜索二叉树,那么就不用往下看了
        }
        if(head.value <= preValue){//如果左树最后处理的一个节点比根节点大,也不用再看了
            return false;
        }else{
            preValue = head.value;//本质上是中序遍历,这一步为处理节点的那一步
        }
        return checkBST(head.right);//如果右树也是搜索二叉树那整棵树都是,如果右树不是那就不是

    }

    public static boolean isBST1(Node head){//非递归的方式实现
        if(head == null){
            return true;
        }
        ArrayList<Node> arr = new ArrayList<>();
        in(head,arr);
        for(int i = 1; i < arr.size(); i++){
            if(arr.get(i).value < arr.get(i - 1).value){
                return false;
            }
        }
        return true;
    }
    public static void in (Node head,ArrayList<Node> arr){
        if(head == null){
            return;
        }
        in(head.left,arr);
        arr.add(head);//相当于中序遍历处理节点那一步,按照中序遍历的方式把节点都加入到数组中
        in(head.right,arr);
    }

    public static boolean isBST2(Node head){//非递归的方式实现
        if(head != null){
            Stack<Node> stack = new Stack<>();
            int preValue = Integer.MIN_VALUE;
            while(!stack.isEmpty() || head != null){//head复用了
                if(head != null){
                    stack.push(head);
                    head = head.left;
                }else{
                    head = stack.pop();
                    if(preValue < head.value){
                        return false;
                    }else{
                        preValue = head.value;
                    }
                    head = head.right;
                }
            }
        }
        return true;
    }
}

总共4种方法,两种递归,2种非递归的
核心思路就是中序遍历二叉树,看是否是升序的
在中序遍历处理节点的那一步进行值比较

新加一种二叉树的递归套路

public class BST{
    public static class Node {
		public int value;
		public Node left;
		public Node right;

		public Node(int data) {
			this.value = data;
		}
	}
  
    public static class Info{//收集信息,1.该树是不是BST 2.该树左树的最大值(因为要求左树最大值小于我)
        public boolean isBST;//3.该树右树的最小值(因为要求右树的最小值大于我)
        public int min;//由于左右两树收集的信息不完全一样,去并集,最小值最大值都收集
        public int max;
        
        public Info(boolean i,int mi,int ma){
            isBST = i;
            min = mi;
            max = ma;
        }
    }
    public static Info process(Node x){
        if(x == null){
            return null;//由于此时为空,Info的isBST虽然能设置为true,但是Min和max不好给值,所以先返回空给下面处理
        }
        Info lefInfo = process(x.left);//收集左树的信息
        Info righInfo = process(x.right);//收集右树的信息

        boolean isBST;//自己也要返回三个信息
        int min;
        int max;

        min = x.value;
        max = x.value;
        if(lefInfo != null){
            min = Math.min(min,lefInfo.min);
            max = Math.max(max,lefInfo.max);
        }
        if(righInfo != null){
            min = Math.min(min, righInfo.min);
            max = Math.max(max, righInfo.max);
        }

        isBST = true;//先设置为true,如果违规设置为false
        if(lefInfo != null && (!lefInfo.isBST || lefInfo.max >= x.value)){
            isBST = false;
        }
        if(righInfo != null && (!righInfo.isBST || righInfo.min <= x.value )){
            isBST = false;
        }

        return new Info(isBST,min,max);
    }

    
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值