面试题32 – I. 从上到下打印二叉树
层次遍历,借助一个队列
/**
* 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
if(root==nullptr) return {};
queue
q.push(root);
vector
while(!q.empty()){
TreeNode*temp=q.front();
res.push_ba
共有 0 条评论