简体   繁体   English

二叉树的级别顺序遍历

[英]level order traversal of binary tree

I am writing the c++ function for level order traversal of a binary tree, 我正在编写用于二叉树的级别顺序遍历的c ++函数,

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. 1)维护一个队列,每个级别的节点都被推入,并在每个级别的最后一个元素之后推送一个NULL指针。 This algorithm gets started by pushing the root element into the queue, followed by a NULL . 通过将根元素推入队列,然后是NULL ,可以启动此算法。

2) Each level's element is popped off the queue, and it's children are pushed back into the queue, immediately. 2)每个级别的元素都从队列中弹出,并且它的子级被立即推回到队列中。

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. 3)当NULL指针从队列中弹出时,它必须意味着队列没有充满下一级别的元素,因此NULL会立即被推回到队列中。

4) The code first collects all elements that come off the queue into a temporary buffer v . 4)代码首先将从队列中出来的所有元素收集到临时缓冲区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. 当一个NULL指针从队列中弹出时, v现在包含二叉树中单个级别上的所有元素,并且vpush_back()编辑为res ,即此函数的返回值。

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. 这里的意图是队列中的最后一个,最后一个剩余值始终是树中最低级别的最后一个值末尾的NULL指针。 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: 不幸的是,该算法已经收集了v向量中树中最低级别的所有值,并且完全跳过了保存它们的以下代码段:

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

The push_back() never gets executed for the bottom level in the tree. push_back()永远不会在树中的底层执行。 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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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