Cattle off - the binary print into multiple lines.

Title Description
top to bottom, binary print layer, the same layer as the output node from left to right. Each line of output layer
Solution:
This problem may be a reference to the blog, delete the row parity determination, can be replaced by two queues

class Solution {
public:
        vector<vector<int> > Print(TreeNode* pRoot) {
            vector<vector<int> > array;
            vector<int> line;
            queue<TreeNode*> Q1;
            queue<TreeNode*> Q2;
            TreeNode* tmp;
            
            if (pRoot==NULL)
                return array;
            
            Q1.push(pRoot);
            while (!Q1.empty())
            {
                line.clear();
                while (!Q1.empty())
                {
                    tmp = Q1.front();
                    line.push_back(tmp->val);
                    Q1.pop();
                    if (tmp->left)
                        Q2.push(tmp->left);
                    if (tmp->right)
                        Q2.push(tmp->right);
                }
                while (!Q2.empty())
                {
                    Q1.push(Q2.front());
                    Q2.pop();
                }
                array.push_back(line);
            }
            return array;
        }
};
Published 315 original articles · won praise 119 · views 110 000 +

Guess you like

Origin blog.csdn.net/w144215160044/article/details/104960842