题解 | #从上往下打印二叉树#
从上往下打印二叉树
http://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701
保存每一层存在的节点在queue里面,依次判断打印,
/* 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) { vector<int> ret; if(!root) return ret; queue<TreeNode*> q; q.push(root); while(!q.empty()){ TreeNode* node=q.front(); q.pop(); ret.push_back(node->val); if(node->left) q.push(node->left); if(node->right) q.push(node->right); } return ret; } };