7-28 搜索树判断 (25 分)

题目链接

这是一个二叉搜索树的题,看以前写的代码,当初竟然不知道OJ上提交java代码不能带包,有点折。

这个题写了好久,感觉挺简单的(写完之后确实挺简单的),值是过程有点不大好,一开始理解错题意了。

因为理解错题意了,后来就改代码,改的很乱,很乱。

思路:就是建两棵树,一棵树是普通二叉树(左子树严格小于,右子树大于等于),另一棵树是题目中的镜像二叉树,然后比较两次就可以了。


import java.util.Scanner;

import javax.security.auth.login.FailedLoginException;
import javax.sql.rowset.BaseRowSet;
import javax.swing.RootPaneContainer;
import javax.swing.text.DefaultEditorKit.InsertBreakAction;
import javax.xml.crypto.Data;

public class Main{
	static int n;
	static int[] num  =new int[100005];
	
	static class node {
		int key;
		node leftchild = null;
		node rightchild = null;
	}
	node root;
	node root2;
	public boolean insert(node data){
        if(root == null){
            root = data;
            return true;
        }
        node current = root;
        while(current != null){
            if(data.key >= current.key){
                if(current.rightchild == null) {
                    current.rightchild = data;
                    return true;
                }
                current =current.rightchild;
            }
            else{
                if(current.leftchild == null) {
                    current.leftchild = data;
                    return true;
                }
                current = current.leftchild;
            }
        }
        return false;
    }
	public boolean insert2(node data){
        if(root2 == null){
            root2 = data;
            return true;
        }
        node current = root2;
        while(current != null){
            if(data.key >= current.key){
               if(current.leftchild == null) {
                    current.leftchild = data;
                    return true;
                }
                current = current.leftchild;
            }
            else{
                
                if(current.rightchild == null) {
                    current.rightchild = data;
                    return true;
                }
                current =current.rightchild;
            }
        }
        return false;
    }
	static int fuck;
	public void preorder_iterator(node data) {  // 前序遍历
		if(fuck==0)
			{	System.out.print(data.key );
				fuck=1;
			}
		else System.out.print(" " +data.key);
		if (data.leftchild != null)
            this.preorder_iterator(data.leftchild);
        if (data.rightchild != null)
            this.preorder_iterator(data.rightchild);
    }
	public void postorder_iterator(node data){ // 后序遍历
        if(data.leftchild != null)
            this.postorder_iterator(data.leftchild);
        if(data.rightchild != null)
            this.postorder_iterator(data.rightchild);
        if(fuck==0){
        	System.out.print(data.key);
        	fuck =1;
        }
        else{
        System.out.print(" "+data.key);
        }
    }
	
	static int cnt ;
	static int flag;
	public boolean checkpre(node data){
		
		if(num[cnt++] != data.key){
			flag=0;
			return false;
		}
		if(data.leftchild != null)
			this.checkpre(data.leftchild);
		if(data.rightchild != null)
			this.checkpre(data.rightchild);
		if(flag==0) return false;
		else return true;
	}
	public boolean checkpos(node data){
		
		if(data.leftchild !=null){
			this.checkpos(data.leftchild);
		}
		if(data.rightchild !=null)
			this.checkpos(data.rightchild);
		if(num[cnt++] != data.key){
			flag=0;
		}
		if(flag==0) return false;
		else return true;
	}
	public static void main(String[] args) {
		Main bst = new Main();
		Scanner in = new Scanner(System.in);
		n = in.nextInt();
		for(int i=0;i<n;i++){
			num[i] = in.nextInt();
			node s = new node();
			node k = new node();
			s.key = num[i];
			s.leftchild = null;
			s.rightchild = null;
			k.key = num[i];
			k.leftchild = null;
			k.rightchild = null;
			bst.insert(s);
			bst.insert2(k);
			//System.out.println("Ssssssss");
		}
		int sss = 0;
		flag =1;
		cnt = 0;
		if(bst.checkpre(bst.root)){
			System.out.println("YES");
			sss = 1;
			fuck = 0;
			bst.postorder_iterator(bst.root);
		}
		cnt=0;
		flag =1;
		if(bst.checkpre(bst.root2) && sss==0){
			
			System.out.println("YES");
			sss=1;
			fuck = 0;
			bst.postorder_iterator(bst.root2);
		}
		if(sss==0){
			System.out.println("NO");
		}
		in.close();
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值