Answers for "level order tree travarsel"

C++
0

level order tree travarsel

class Node{
public:
    long val = 0;
    Node *left = nullptr, *right = nullptr;
    Node(int val_): val(val_){}
};

void level_order_traversal(Node *root=nullptr){
    // printf("Level Order Tree Traversal: \n");
    if(not root) return;

    queue<Node*> Q;
    Q.push(root);
    Q.push(nullptr);

    while(Q.size() > 1){
        Node *current = Q.front();
        Q.pop();

        if(not current){
            printf("\n");
            Q.push(nullptr);
            continue;
        }
        printf("%ld ", current->val);

        if(current->left) Q.push(current->left);
        if(current->right) Q.push(current->right);
    }

    printf("\n\n");
}
Posted by: Guest on August-06-2021

Code answers related to "level order tree travarsel"

Browse Popular Code Answers by Language