Why its not printing kth level
Is your tree getting built correctly ?
#include<iostream>
using namespace std;
class node{
public:
int data;
node *left;
node *right;
node(int d){
data=d;
left=NULL;
right=NULL;
}
};
node * buildtree(){
int d;
cin>>d;
if(d==-1)
return NULL;
node *root=new node(d);
root->left=buildtree();
root->right=buildtree();
return root;
}
void printKthlevel(node *root,int k){
if(root==NULL)
return;
if(k==1){
cout<<root->data<<" ";
}
printKthlevel(root->left,k-1);
printKthlevel(root->right,k-1);
return;
}
int main(){
node *root=buildtree();
printKthlevel(root,2);
}
Your tree was not getting built properly . I made some changes and now it is working properly.