AcWing 43. 不分行从上往下打印二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector printFromTopToBottom(TreeNode* root) {
        queue q;
        vector r;
        q.push(root);
        while(!q.empty()){
            TreeNode* f=q.front();
            q.pop();
            if(f!=NULL) r.push_back(f->val),q.push(f->left),q.push(f->right);
        }
        return r;
    }
};

你可能感兴趣的:(算法)