简体   繁体   中英

level order traversal of binary tree

I am writing the c++ function for level order traversal of a binary tree,

but it is not printing all levels, can somebody tell what is the problem with this code ? here is my code : ##

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector< vector<int> > res;
        TreeNode* ptr;
        queue<TreeNode*> q;
        vector<int> v;            

        if(root){
            q.push(root);
            q.push(NULL);
        }

        while(!(q.empty())){
            ptr = q.front();
             q.pop();

            if(q.empty())
                break;

            if(ptr){
                    if(ptr->left)
                        q.push(ptr->left);
                    if(ptr->right)
                        q.push(ptr->right);
                    v.push_back(ptr->val);
                }
            else {
                    q.push(NULL);
                    res.push_back(v);
                    v.clear();
                }                                    
        }            
        return res;                              
    }
};

To summarize the overall logic in this code:

1) Maintain a queue where the nodes at each level get pushed into, and push a NULL pointer after the last element on each level. This algorithm gets started by pushing the root element into the queue, followed by a NULL .

2) Each level's element is popped off the queue, and it's children are pushed back into the queue, immediately.

3) When a NULL pointer gets popped off the queue, it must mean that the queue not is full of the next level's element, so a NULL gets immediately pushed back onto the queue.

4) The code first collects all elements that come off the queue into a temporary buffer v . When a NULL pointer gets popped off the queue, v now contains all elements on a single level in the binary tree, and v gets push_back() ed into res , the return value from this function.

The bug occurs in this section of the code:

        ptr = q.front();
        q.pop();

        if(q.empty())
            break;

This removes the next element from the queue. The algorithm terminates if the queue is empty after the element is removed.

The intent here is that the last, final, remaining value in the queue is always the NULL pointer at the end of the last value on the lowest level in the tree. The queue is now completely drained, and the algorithm terminates.

Unfortunately, the algorithm has collected all the values from the lowest level in the tree in the v vector, and the following section of code, that saves them, gets completely skipped:

                q.push(NULL);
                res.push_back(v);
                v.clear();

The push_back() never gets executed for the bottom level in the tree. The final result from this function will never include the lowest level in the binary tree.

Change the terminating condition to:

        if(q.empty())
        {
            res.push_back(v);
            break;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM