- C++
#求助!😭😭😭
- @ 2026-1-31 13:16:42
#include<bits/stdc++.h>
using namespace std;
int g=0,n,cen=1,c=0;
struct Node {
int data;
Node* left, *right;
};
Node* build() {
int x; cin >> x;
if (x == -1) return nullptr;
Node* node = new Node();
node->data = x;
node->left = build();
node->right = build();
return node;
}int getTreeDepth(Node* root) {
if (root == nullptr) {
return 0;
}
int leftDepth = getTreeDepth(root->left);
int rightDepth = getTreeDepth(root->right);
return 1 + std::max(leftDepth, rightDepth);
}
int bu(Node* root) {
if(cen==n&&root!=0) return 1;
else{
cen++;
g+=bu(root->left);
g+=bu(root->right);
cen--;
} return g;
}
int main() {
Node* root = build();
cin>>n;
if(n<1||n>getTreeDepth(root)){
cout<<0;
}else{
bu(root);
cout<<g;
}
}
0 条评论
目前还没有评论...