The code which I have written gives the correct output. I just wanted to know if this method will run into problems with larger input sizes.
#include <bits/stdc++.h>
using namespace std;
//LEVEL ORDER TRAVERSAL
class node{
private: int data;
public : node* left=NULL;node *right=NULL;
node(int dat){
data=dat;
}
int getVal(){
return data;
}
};
void level_order(node *a,queue<node*> q){
if(a->left!=NULL){
q.push(a->left);
}
if(a->right!=NULL){
q.push(a->right);
}
cout<<a->getVal()<<",";
q.pop();
if(!q.empty()){
level_order(q.front(),q);}
}
int main(){
node* root=new node(1);
root->left=new node(2);
root->right=new node(3);
root->left->left=new node(4);
root->left->right=new node(5);
root->left->right->left=new node(6);
root->right->left=new node(7);
root->right->right=new node(8);
root->right->right->left=new node(9);
root->right->right->right=new node(10);
queue<node*> a;
a.push(root);
level_order(root,a);
return 0;
}
I am just getting started with Data Structures. Sorry if this seems like a silly question.