剑指offer | 训练题59:把二叉树打印成多行
2017-07-05 21:28
344 查看
题目描述
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。思路
//思路:BFS /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } }; */ class Solution { public: vector<vector<int> > Print(TreeNode* pRoot) { vector<vector<int> > ans; if(pRoot == NULL) return ans; queue<TreeNode*> q; q.push(pRoot); while(!q.empty()){ int size = q.size();//读取每一层的元素的数量 vector<int> levelelem; while(size--){ TreeNode* t = q.front(); q.pop(); levelelem.push_back(t->val); if(t->left != NULL) q.push(t->left); if(t->right != NULL) q.push(t->right); } ans.push_back(levelelem); } return ans; } };
相关文章推荐
- 剑指offer——60.把二叉树打印成多行
- 剑指offer—把二叉树打印成多行
- 剑指offer----把二叉树打印成多行----java实现
- (C++)剑指offer-59:按之字形顺序打印二叉树(树)
- 剑指offer-----多行打印二叉树(java版)
- 剑指Offer--060-把二叉树打印成多行
- 剑指offer_二叉树---把二叉树打印成多行
- 剑指offer-把二叉树打印成多行-php
- 剑指Offer面试题60:把二叉树打印成多行 Java实现
- (C++)剑指offer-60:把二叉树打印成多行(树)
- 剑指offer:把二叉树打印成多行
- 【剑指Offer】把二叉树打印成多行
- 剑指offer(C++)——把二叉树打印成多行
- 剑指offer61题(把二叉树打印成多行)
- 剑指offer-59.按之字形顺序打印二叉树
- 剑指offer-60.把二叉树打印成多行
- 剑指offer — 把二叉树打印成多行
- 【剑指Offer】面试题60:把二叉树打印成多行
- 剑指Offer——把二叉树打印成多行
- 【剑指offer-解题系列(61)】把二叉树打印成多行