Created
May 10, 2016 00:52
-
-
Save jianminchen/a3194f885fc542a7f5827cbcdc92a3f5 to your computer and use it in GitHub Desktop.
HackerRank - Tree - Level Order Traversal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
struct node | |
{ | |
int data; | |
node* left; | |
node* right; | |
}*/ | |
#include <queue> | |
using namespace std; | |
void LevelOrder(node * root) | |
{ | |
if(root==NULL) | |
return; | |
queue<node*> myQueue ; | |
myQueue.push(root); | |
while(!myQueue.empty()) | |
{ | |
node* cur = myQueue.front(); | |
myQueue.pop(); | |
printf("%d ", cur->data); | |
if(cur->left!=NULL) | |
myQueue.push(cur->left); | |
if(cur->right!=NULL) | |
myQueue.push(cur->right); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment