剑指 Offer 32

题意

从上到下打印二叉树的每一行,最后返回一个层序遍历的序列

思路

  • 使用队列作为辅助工具来进行层序遍历,都存储到一个vector里面返回即可

代码

class Solution {
public:
    vector<int> levelOrder(TreeNode* root) {
        if(!root) {
            return {};
        }
        queue<TreeNode*> q;
        vector<int> ans;
        q.push(root);
        while(!q.empty()) {
            auto cur = q.front();
            q.pop();
            ans.emplace_back(cur->val);
            if(cur->left) {
                q.push(cur->left);
            }
            if(cur->right) {
                q.push(cur->right);
            }
        }       
        return ans;
    }
};
如有转载,请注明出处QAQ
原文地址:https://www.cnblogs.com/MartinLwx/p/14347162.html