九度oj 题目1009:二叉搜索树

21 篇文章 0 订阅
<dt class="title-hd" style="margin: 0px; padding: 5px 10px 0px; height: 35px; color: rgb(68, 69, 69); border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(187, 187, 187); font-weight: 700; font-size: 18px; line-height: 35px; font-family: 'Microsoft Yahei'; background-image: url(http://ac.jobdu.com/css/images/bg_title_repeat.gif); background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: repeat-x;">题目1009:二叉搜索树</dt><dd class="title-bd" style="margin: 0px; padding: 25px 0px; color: rgb(51, 51, 51); font-family: 'MicroSoft Yahei', Helvetica, Arial, Georgia, Simsun; font-size: 14px; line-height: 21px;"><div class="pl20 mb10 f14 topic-desc-mod" style="margin-bottom: 10px; padding-left: 20px; font-size: 14px; position: relative; width: 745px; overflow: hidden;"><div class="topic-desc-hd" style="margin-bottom: 10px; width: 800px; height: 32px; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: rgb(187, 187, 187); overflow: hidden;"><p style="margin-top: 0px; margin-right: 10px; margin-bottom: 5px; padding: 0px 5px; float: left; height: 25px; width: 140px; line-height: 25px; border: 1px solid rgb(212, 212, 212);"><span style="float: left;">时间限制:</span>1 秒</p><p style="margin-top: 0px; margin-right: 10px; margin-bottom: 5px; padding: 0px 5px; float: left; height: 25px; width: 140px; line-height: 25px; border: 1px solid rgb(212, 212, 212);"><span style="float: left;">内存限制:</span>32 兆</p><p style="margin-top: 0px; margin-right: 10px; margin-bottom: 5px; padding: 0px 5px; float: left; height: 25px; width: 100px; line-height: 25px; border: 1px solid rgb(212, 212, 212);"><span style="float: left;">特殊判题:</span>否</p><p style="margin-top: 0px; margin-right: 10px; margin-bottom: 5px; padding: 0px 5px; float: left; height: 25px; width: 130px; line-height: 25px; border: 1px solid rgb(212, 212, 212);"><span style="float: left;">提交:</span>4422</p><p style="margin-top: 0px; margin-right: 10px; margin-bottom: 5px; padding: 0px 5px; float: left; height: 25px; width: 130px; line-height: 25px; border: 1px solid rgb(212, 212, 212);"><span style="float: left;">解决:</span>1982</p></div><div class="topic-desc-bd" style="line-height: 25px;"><dl style="margin: 0px 0px 20px; padding: 0px;"><dt style="margin: 0px 0px 10px; padding: 0px; font-size: 16px;"><strong>题目描述:</strong></dt><dd style="margin: 0px; padding: 0px; font-size: 14px;"><div class="panel_content">判断两序列是否为同一二叉搜索树序列</div></dd></dl><dl style="margin: 0px 0px 20px; padding: 0px;"><dt style="margin: 0px 0px 10px; padding: 0px; font-size: 16px;"><strong>输入:</strong></dt><dd style="margin: 0px; padding: 0px; font-size: 14px;"><div class="panel_content">开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。</div></dd></dl><dl style="margin: 0px 0px 20px; padding: 0px;"><dt style="margin: 0px 0px 10px; padding: 0px; font-size: 16px;"><strong>输出:</strong></dt><dd style="margin: 0px; padding: 0px; font-size: 14px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px;">如果序列相同则输出YES,否则输出NO</p></dd></dl><dl style="margin: 0px 0px 20px; padding: 0px;"><dt style="margin: 0px 0px 10px; padding: 0px; font-size: 16px;"><strong>样例输入:</strong></dt><dd style="margin: 0px; padding: 0px; font-size: 14px;"><pre style="white-space: pre-wrap;">2
567432
543267
576342
0
样例输出:
YES
NO
思路:按照二叉搜索树的产生方式输入的序列生成到一维数组里面,比较生成的序列一样与否即可
注意:数组的长度要开的足够大

 
import java.io.BufferedInputStream;
import java.util.Scanner;


public class Main1009 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner cin=new Scanner(new BufferedInputStream(System.in));
		char ch1[];
		int ch2[],ch3[];
		String s;
		int n,i,j,len=1024;
		int flag,m;
		while(true){
		n=cin.nextInt();
		  if(n==0)
			break;
		s=cin.next();
		ch1=s.toCharArray();
		ch2=new int[len];
		ch3=new int[len];
		for(i=0;i<len;i++){
			ch2[i]=-1;
		}
        
         
       for(i=0;i<s.length();i++){
    	   m=ch1[i]-'0';
    	   j=0;
    	   while(ch2[j]!=-1){
    		   if(m<ch2[j])
    			   j=(j+1)*2-1;
    		   if(m>ch2[j])
    			   j=(j+1)*2;
    	   }
    	   ch2[j]=m;
       }
       while(n-->0){
    	  s=cin.next();
    	 // System.out.println(s);
   		ch1=s.toCharArray();
   		
   		for(i=0;i<len;i++){
   			ch3[i]=-1;
   		}
           
            
          for(i=0;i<s.length();i++){
       	   m=ch1[i]-'0';
       	   j=0;
       	   while(ch3[j]!=-1){
       		   if(m<ch3[j])
       			   j=(j+1)*2-1;
       		   if(m>ch3[j])
       			   j=(j+1)*2;
       	   }
       	   ch3[j]=m;
          }
          
          flag=0;
          for(i=0;i<len;i++){
       	   if(ch2[i]!=ch3[i]){
       		   flag=1;
       		   break;
       	   }
          }
         // System.out.println(flag);
          if(flag==0){
       	   System.out.println("YES");
          }else{
       	   System.out.println("NO");
          }
       }
       
      }
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值