Skip to content

Latest commit

 

History

History
81 lines (61 loc) · 2.11 KB

File metadata and controls

81 lines (61 loc) · 2.11 KB

032_02-把二叉树打印成多行

tags: 层次遍历 二叉树


题目原文

牛客网链接

从上到下按层打印二叉树, 同一层的节点按从左到右的顺序打印, 每一层打印到一行, 例如, 下图中二叉树的打印结果为

1562160030348

解题思路

1562160041242

1562160051709

代码

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
        vector<vector<int> > Print(TreeNode* pRoot) {
            vector<vector<int>> res;
            if(pRoot==nullptr)
                return res;
            int currentLevelNum=1;
            int nextLevelNum=0;
            queue<TreeNode*> nodeQueue;
            nodeQueue.push(pRoot);
            vector<int> level;
            while(!nodeQueue.empty()){
                TreeNode* node=nodeQueue.front();
                nodeQueue.pop();
                currentLevelNum--;
                level.push_back(node->val);
                
                if(node->left!=nullptr){
                    nodeQueue.push(node->left);
                    nextLevelNum++;
                }
                
                if(node->right!=nullptr){
                    nodeQueue.push(node->right);
                    nextLevelNum++;
                }
                
                if(currentLevelNum==0){
                    res.push_back(level);
                    vector<int>().swap(level);
                    currentLevelNum=nextLevelNum;
                    nextLevelNum=0;
                }
            }
            
            return res;
        }
    
};