7-4 是否同一棵二叉搜索树 (25 分)

题目链接

这个题我一开始用Java写的,感觉没什么毛病,能想到的样例都能过,但是提交到OJ上一个点都过不了,我真懵了,如果有大佬路过的话希望帮我改一下,然后我又用C++写了一个就过了。。。。。。折。

思路:就是建一颗二叉搜索树,然后一个点一个点的对比,如果整棵树有一样的话那就是一样,如果有一点不一样就是不一样。

这是C++代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <queue>
using namespace std;
struct node{
    node* leftchild;
    node* rightchild;
    int key;
    node(int n){
        key = n;
        leftchild= NULL;
        rightchild=NULL;
    }
};
typedef node* Tree;
Tree t1,t2;
void Insert(Tree &T,int n){
    if(T == NULL){
        T = new node(n);
        return ;
    }
    if(n>T->key){
        Insert(T->rightchild,n);
    }
    else{
        Insert(T->leftchild,n);
    }
}
bool Compare(Tree bst1,Tree bst2){
    queue<Tree> q1;
    queue<Tree> q2;
    q1.push(bst1);
    q2.push(bst2);
    while(!q1.empty() && !q2.empty()){
        Tree f1 = q1.front();
        Tree f2 = q2.front();
        q1.pop();
        q2.pop();
        if(f1->key != f2->key){
            return false;
        }
        if (f1 -> leftchild != NULL)
			q1.push(f1 -> leftchild);
		if (f1 -> rightchild != NULL)
			q1.push(f1 -> rightchild);
		if (f2 -> leftchild != NULL)
			q2.push(f2 -> leftchild);
		if (f2 -> rightchild != NULL)
			q2.push(f2 -> rightchild);
    }
    if(q1.empty() && q2.empty()){
        return true;
    }
    else return false;
}
int main(){
    int N,T;
    int key;
    while(cin>>N && N){
            t1=NULL;
        cin>>T;
        for(int i=1;i<=N;i++){
            cin>>key;
            Insert(t1,key);
        }
       
        for(int j=1;j<=T;j++){
                t2=NULL;
            for(int i=1;i<=N;i++){
                cin>>key;
                Insert(t2,key);
            }
            if(Compare(t1,t2))
                printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}

这个是Java代码(一个点都过不去 - -):

package com.company;
import java.util.Scanner;
public class Main {
    static class node{
        public int key;
        public node leftchild = null;
        public node rightchild = null;
    }
    node root;
    public nodea find(int key){
        if(root == null){
            return null;
        }
        node current = root;
        while(current.key != key){
            if(key > current.key)
                current = current.rightchild;
            else current = current.leftchild;
            if(current == null)
                return null;
        }
        return current;
    }
    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 void inorder_iterator(node data){
        if(data.leftchild != null)
            this.inorder_iterator(data.leftchild);
        System.out.print(data.key + "");
        if(data.rightchild != null)
            this.inorder_iterator(data.rightchild);
    }

    public static boolean CompareBST(node node1,node node2){
        if(node1 == null && node2 == null)
            return true;
        else if(node1 == null && node2 != null)
            return false;
        else if(node1 != null && node2 == null)
            return false;
        if(node1.key != node2.key ) return false;
        else{
            boolean flag1 = false;
            boolean flag2 = false;
            flag1 = CompareBST(node1.leftchild,node2.leftchild);
            if(flag1 == true){
                flag2 = CompareBST(node1.rightchild , node2.rightchild);
                if(flag2) return true;
                else return false;
            }
            else return false;
        }
    }

    public static void main(String[] args) {
        int N,L;
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){
            Main bst = new Main();
            N = in.nextInt();
            if(N == 0 ) break;
            L = in.nextInt();
            for(int i=1;i<=N;i++){
                node a = new node();
                a.key = in.nextInt();
                bst.insert(a);
            }
            for(int j=1;j<=L;j++){
                Main bstToCom = new Main();
                for(int i=1;i<=N;i++){
                    node b = new node();
                    b.key = in.nextInt();
                    bstToCom.insert(b);
                }
                if(CompareBST(bst.root,bstToCom.root))
                    System.out.println("Yes");
                else System.out.println("No");
            }
        }
    }
}

哎,java简单还是麻烦呢 - -。

不知道。

猜你喜欢

转载自blog.csdn.net/qq_40941611/article/details/82847094