1002. 计算二叉查找树的高度

本文讨论了在使用DevCPP编程时,如果多个代码文件名过于相似可能会引发的错误,以及如何避免和解决这类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

devcpp代码名字如果太相近就会出现bug

#include <iostream>
using namespace std;
const int maxn = 100;
int a[maxn],b[maxn];
struct TreeNode {
    int val;
    TreeNode* left,*right;
    TreeNode(int a):val(a),left(NULL),right(NULL) {}
};

int height(TreeNode* root) {
    if (root==NULL) return 0;
    else return max(height(root->left),height(root->right))+1;
}


TreeNode* f(int l1,int r1,int l2,int r2) {
    if (l2<=r2) {
        TreeNode* root = new TreeNode(a[l1]);
        int i=l2,cmp=0;
        while(i<=r2&&a[l1]!=b[i]) {
            i++; cmp++;
        }
        if (i>r2) return NULL;
        root->left = f(l1+1,l1+cmp,l2,i-1);
        root->right= f(l1+cmp+1,r1,i+1,r2);
//      cout<<a[l1]<<" ";
        return root;
    } else return NULL;
}




int main() {
    int t;
    cin>>t;
    while(t--) {
        int n;
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=1;i<=n;i++) cin>>b[i];
        TreeNode* root = f(1,n,1,n);
        cout<<height(root)-1<<endl;

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值