Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

level order traversal of binary tree c++

/*
node class contains:
node* left;
node* right;
int value;
*/
void levelOrder(node* root){
  node* ptr=nullptr;
  queue<node*> q;
  q.push(root);
  while(!q.empty()){
    ptr=q.front();
    cout<<ptr->value<<endl;
    if(ptr->left!=nullptr){
q.push(ptr->left);
  }
    if(ptr->right!=nullptr){
    q.push(ptr->right);
    }
    q.pop();
  }
}
Source by www.techiedelight.com #
 
PREVIOUS NEXT
Tagged: #level #order #traversal #binary #tree
ADD COMMENT
Topic
Name
2+3 =