题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: vector<int> PrintFromTopToBottom(TreeNode* root) { queue<TreeNode*>q; vector<int>vt; if(root == NULL) return vt; q.push(root); vt.push_back(root->val); while(!q.empty()){ TreeNode* tmp = q.front(); q.pop(); if(tmp->left != NULL){ vt.push_back(tmp->left->val); q.push(tmp->left); } if(tmp->right != NULL){ vt.push_back(tmp->right->val); q.push(tmp->right); } } return vt; } };