C++ with BFS


  • 0
    T

    '''
    class Solution {
    public:
    void connect(TreeLinkNode root) {
    if (!root) return;
    queue<TreeLinkNode
    > bucket;
    bucket.push(root);

        while (!bucket.empty()){
            bucket.push(NULL);
            int length = bucket.size();
            for (int i=0; i < length-1; i++){
                if (bucket.front() != NULL) {
                    TreeLinkNode* curr = bucket.front();
                    bucket.pop();
                    curr->next = bucket.front();
                    if (curr->left) bucket.push(curr->left);
                    if (curr->right) bucket.push(curr->right);
                }
            }
            if (bucket.front() == NULL){
                bucket.pop();
            }
        }
    }
    

    };
    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.