thafnhlong / hackerrank-storage

0 stars 0 forks source link

https://www.hackerrank.com/challenges/tree-level-order-traversal #17

Open thafnhlong opened 2 years ago

thafnhlong commented 2 years ago

Use queue :)

Vì left/right có thể null nên cẩn thận

    void levelOrder(Node * root) {
        queue<Node*> db;
        db.push(root); 
        while(!db.empty()){
            Node* cur = db.front(); db.pop();
            if (cur == nullptr){continue;}
            cout << cur->data << " ";
            db.push(cur->left);db.push(cur->right);
        }
    }