标签:off style ack size nod vector font push tor
【思路】使用队列实现二叉树的层次遍历。
1 /* 2 struct TreeNode { 3 int val; 4 struct TreeNode *left; 5 struct TreeNode *right; 6 TreeNode(int x) : 7 val(x), left(NULL), right(NULL) { 8 } 9 }; 10 */ 11 class Solution 12 { 13 public: 14 vector<vector<int> > Print(TreeNode* pRoot) 15 { 16 vector<vector<int> > res; 17 if(pRoot == NULL) 18 return res; 19 queue<TreeNode*> Que; 20 Que.push(pRoot); 21 while(!Que.empty()) 22 { 23 vector<int> vec; 24 int size = Que.size(); 25 for(int i = 0; i < size; i ++) 26 { 27 TreeNode* temp = Que.front(); 28 vec.push_back(temp->val); 29 if(temp->left) 30 Que.push(temp->left); 31 if(temp->right) 32 Que.push(temp->right); 33 Que.pop(); 34 } 35 res.push_back(vec); 36 } 37 return res; 38 } 39 };
标签:off style ack size nod vector font push tor
原文地址:http://www.cnblogs.com/lca1826/p/6574243.html