level order tree travarsel

Solutions on MaxInterview for level order tree travarsel by the best coders in the world

showing results for - "level order tree travarsel"
Tidiane
09 Sep 2019
1class Node{
2public:
3    long val = 0;
4    Node *left = nullptr, *right = nullptr;
5    Node(int val_): val(val_){}
6};
7
8void level_order_traversal(Node *root=nullptr){
9    // printf("Level Order Tree Traversal: \n");
10    if(not root) return;
11
12    queue<Node*> Q;
13    Q.push(root);
14    Q.push(nullptr);
15
16    while(Q.size() > 1){
17        Node *current = Q.front();
18        Q.pop();
19
20        if(not current){
21            printf("\n");
22            Q.push(nullptr);
23            continue;
24        }
25        printf("%ld ", current->val);
26
27        if(current->left) Q.push(current->left);
28        if(current->right) Q.push(current->right);
29    }
30
31    printf("\n\n");
32}
similar questions
queries leading to this page
level order tree travarsel