题解 | #从上往下打印二叉树#
//这道题就是个层序遍历。 #include <queue> #include <vector> class Solution { public: vector<int> PrintFromTopToBottom(TreeNode* root) { vector<int>result; if(root==nullptr) return result; queue<TreeNode*>q; q.push(root); while (!q.empty()) { TreeNode *top=q.front(); q.pop(); result.emplace_back(top->val); if (top->left) { q.push(top->left); } if(top->right) { q.push(top->right); } } return result; } };