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简单还是麻烦呢 - -。

不知道。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 题目描述: 给定两个整数序列,别表示两棵二叉搜索树的中序遍历序列,判断这两棵二叉搜索树是否一棵。 解题思路: 二叉搜索树的中序遍历序列是有序的,因此我们可以将两个序列排序后比较是否相等。如果相等,则说明两棵二叉搜索树相同。 代码实现: ``` bool isSameBST(vector<int>& seq1, vector<int>& seq2) { if (seq1.size() != seq2.size()) { return false; } sort(seq1.begin(), seq1.end()); sort(seq2.begin(), seq2.end()); return seq1 == seq2; } ``` 时间复杂度:$O(nlogn)$ 空间复杂度:$O(n)$ 其中,$n$ 表示二叉搜索树中节点的个数。 ### 回答2: 题目描述: 给定两个长度相等的整型数组,判断它们是否对应同一棵二叉搜索树的后序遍历序列。 解题思路: 本题的难点在于如何判断两个数组是否对应同一棵二叉搜索树的后序遍历序列。 首先,我们知道二叉搜索树的后序遍历序列的最后一个元素为根节点,而且比根节点小的元素在左子树中,比根节点大的元素在右子树中。 因此,我们可以将数组前面的元素为左子树和右子树两个部,然后在子树中递归检查,直到只剩下一个元素为止。 具体实现上,我们可以先找到根节点,然后将数组根据根节点为左右两个子数组,再递归检查左右子树是否对应同一棵二叉搜索树的后序遍历序列。 如果左右子树都满足条件,那么说明整个数组对应同一棵二叉搜索树的后序遍历序列,否则不是。 Code: ### 回答3: 题目描述: 给定两个长度为n的数组,按照给定的顺序插入到一棵空的二叉搜索树中,判断两个数组是否最终构建了同一棵二叉搜索树。 思路析: 本题是一道典型的二叉搜索树的应用题,题目要求我们判断两个经过插入操作后最终构建的树是否相等。 首先回顾一下二叉搜索树的特点:对于一个二叉搜索树,它的左子树中的所有节点的值都小于根节点的值,而它右子树中的所有节点的值都大于根节点的值。 对于这个问题来说,我们可以在构建二叉搜索树的同时统计每个节点的左子树大小来判断两棵树是否相同。 具体来说,对于每个节点,我们可以统计它左子树中的节点数目,然后每插入一个节点就更新一遍。当构建完毕后,每个节点的左子树大小就都已经确定了。然后判断两个构建好的树中所有节点的左子树大小是否都一一对应即可。 代码实现: 为了方便起见,我们可以先实现一个二叉搜索树的插入操作,然后再利用这个代码来对两个输入排序数组进行构建。 //二叉搜索树结构体 struct TreeNode { int val; int left_size;//记录左子树大小的变量 TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left_size(0), left(NULL), right(NULL) {} }; TreeNode* insert(TreeNode* root, int val){ if(!root) return new TreeNode(val); if(val < root->val){ root->left = insert(root->left, val); ++root->left_size;//更新左子树大小 } else root->right = insert(root->right, val); return root; } 然后我们在主函数中对两个输入数组进行插入操作,之后再进行比较即可。 bool isSameTree(vector<int>& nums1, vector<int>& nums2) { if(nums1.size() != nums2.size()) return false; TreeNode* root1 = nullptr;//第一棵树的根节点 TreeNode* root2 = nullptr;//第二棵树的根节点 for(int i=0; i<nums1.size(); ++i){ root1 = insert(root1, nums1[i]); root2 = insert(root2, nums2[i]); } function<bool(TreeNode*, TreeNode*)> dfs = [&](TreeNode* node1, TreeNode* node2){ if(node1 == nullptr && node2 == nullptr) return true; else if(node1 == nullptr || node2 == nullptr) return false; else if(node1->val != node2->val || node1->left_size != node2->left_size) return false; else return dfs(node1->left, node2->left) && dfs(node1->right, node2->right); }; return dfs(root1, root2); } 时间复杂度:插入节点的时间复杂度是O(logn),所以整体时间复杂度是O(nlogn)。 空间复杂度:树的深度最大为O(n),所以空间复杂度也是O(n)。 参考链接: https://leetcode-cn.com/problems/same-tree/description/ https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/description/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值