Sorry for creating a new theme, but in the past there is nowhere to comment on and a lot of things have been written, but I have to finish it today and I need to finish a little bit. The problem is that it does not work for me to transfer nodes with depth x to my function. Task: For the given values of "x" calculate the number of all nodes with a depth of "x" in a given tree. that is, the question of transferring this value to these functions. The values there are clear how the functions are set, but I did not understand the depths. Explain please, I already understood the rest how to do it, but I don’t understand it at all.
Functions to which I pass:
void sumx (Node *Tree, int x[], int nx ) { for (int i = 0; i < nx; i++) cout << "level: " << x[i] << ' ' << cntx(Tree, x[i]) << "Nodes\n"; } int cntx (Node *Tree, int deep) { if (!Tree || deep < 0) return 0; if (deep == 0) return 1; return cntx(Tree->l, --deep) + cntx(Tree->r, --deep); } весь код: #include <iostream> #include <conio.h> using namespace std; struct Node { int x; Node *l,*r; }; void show(Node *&Tree) { if (Tree!=NULL) { show(Tree->l); cout<<Tree->x; show(Tree->r); } cout << Tree->x << " [" << Tree << " left: " << Tree->l << " right: " << Tree->r << "]\n"; } void add_node(int x,Node *&MyTree) { if (MyTree) { if (x < MyTree->x) add_node(x, MyTree->l); else add_node(x, MyTree->r); } else { MyTree=new Node; MyTree->x=x; MyTree->l=MyTree->r=NULL; } } void sumx (Node *Tree, int x[], int nx ) { for (int i = 0; i < nx; i++) cout << "level: " << x[i] << ' ' << cntx(Tree, x[i]) << "Nodes\n"; } int cntx (Node *Tree, int deep) { if (!Tree || deep < 0) return 0; if (deep == 0) return 1; return cntx(Tree->l, --deep) + cntx(Tree->r, --deep); } void DeleteTree(Node *Tree) { if (Tree) { DeleteTree(Tree->l); DeleteTree(Tree->r); delete Tree; Tree = NULL; } } int main() { Node *Tree=NULL; int z,k; cin >> z; for (int i=0;i<z;i++) { cin >> k; add_node(k,Tree); } show(Tree); DeleteTree(Tree); cin.get(); getch(); return 0; }