day6
1.剑指 Offer 32 - I. 从上到下打印二叉树
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<int> levelOrder(TreeNode* root) { 13 vector<int> res; 14 if(root == NULL) return res; 15 queue<TreeNode*> q; 16 q.push(root); 17 while(!q.empty()){ 18 TreeNode* tmp = q.front(); 19 q.pop(); 20 res.push_back(tmp -> val); 21 if(tmp -> left) q.push(tmp -> left); 22 if(tmp -> right) q.push(tmp -> right); 23 } 24 return res; 25 } 26 };
2.剑指 Offer 32 - II. 从上到下打印二叉树 II
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<int>> levelOrder(TreeNode* root) { 13 vector<vector<int> > res; 14 if(!root) return res; 15 queue<TreeNode*> q; 16 q.push(root); 17 while(!q.empty()){ 18 res.push_back(vector<int>()); 19 int n = q.size(); 20 for(int i = 0;i < n;i ++){ 21 auto tmp = q.front(); 22 q.pop(); 23 res.back().push_back(tmp -> val); 24 if(tmp -> left) q.push(tmp -> left); 25 if(tmp -> right) q.push(tmp -> right); 26 } 27 } 28 return res; 29 } 30 };
3.剑指 Offer 32 - III. 从上到下打印二叉树 III
1)倒序层的情况用了一个栈作为中转来处理
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<int>> levelOrder(TreeNode* root) { 13 vector<vector<int> > res; 14 if(!root) return res; 15 queue<TreeNode*> q; 16 q.push(root);int flag = 0; 17 while(!q.empty()){ 18 res.push_back(vector<int>()); 19 int n = q.size(); 20 if(flag == 0){ 21 for(int i = 0;i < n;i ++){ 22 auto tmp = q.front(); 23 q.pop(); 24 res.back().push_back(tmp -> val); 25 if(tmp -> left) q.push(tmp -> left); 26 if(tmp -> right) q.push(tmp -> right); 27 } 28 flag = 1; 29 } 30 else{ 31 stack<int> stk; 32 for(int i = 0;i < n;i ++){ 33 auto tmp = q.front(); 34 q.pop(); 35 stk.push(tmp -> val); 36 if(tmp -> left) q.push(tmp -> left); 37 if(tmp -> right) q.push(tmp -> right); 38 } 39 while(!stk.empty()){ 40 res.back().push_back(stk.top()); 41 stk.pop(); 42 } 43 flag = 0; 44 } 45 } 46 return res; 47 } 48 };
2)除了用栈还可以用一个整数数组来保存,然后取反放进res数组里
1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<int>> levelOrder(TreeNode* root) { 13 vector<vector<int> > res; 14 if(!root) return res; 15 queue<TreeNode*> q; 16 q.push(root); 17 while(!q.empty()){ 18 // res.push_back(vector<int>());如果直接加数组 这个不能加 19 vector<int> tmp; 20 int n = q.size(); 21 for(int i = 0;i < n;i ++){ 22 auto cur = q.front(); 23 q.pop(); 24 tmp.push_back(cur -> val); 25 if(cur -> left) q.push(cur -> left); 26 if(cur -> right) q.push(cur -> right); 27 } 28 if(res.size() % 2 == 1) reverse(tmp.begin(),tmp.end());//tmp保存的是下一行的数据,所以当余数为1时反转 29 res.push_back(tmp); 30 } 31 return res; 32 } 33 };
28,29行可等替为:
1 if(res.size() % 2 == 1) 2 res.push_back(vector<int>(tmp.rbegin(),tmp.rend())); 3 else 4 res.push_back(tmp);
3)还可用双端队列:
偶数层:倒序,为了保证下一层的元素顺序不乱套,所以只能先遍历右子树再遍历左子树(注:此处所说的遍历是遍历的其子层),例(表格里是一颗完全二叉树):
| 3 | |||
| 9 | 20 | ||
| 6 | 4 | 15 | 7 |
第二行是倒序:20 9
第三行:先右后左:7 15 4 6(倒序)
先左后右:15 7 6 4(乱套了)
偶数层的时候把双端队列当栈使用(后进先出),即 从哪个端口进就从哪个端口出。
判断进口尾端还是首端:上一层奇数层的时候是从哪个口把本层元素压入双端队列的就是从哪端入的
奇数层(root层算第1层):正序,同样的道理,所以奇数层先先遍历左子树再遍历右子树
奇数层的时候也把双端队列当栈使用
因为偶数层是以倒序把其下一层压入队列的(看例子的第三行),但其下一层 即奇数层 输出的应该是正序,所以要反向输出
为什么不直接用栈而用双端队列,因为栈进出只有一个端口,不能一个端口进另一个端口出
;1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<int>> levelOrder(TreeNode* root) { 13 vector<vector<int> > res; 14 if(!root) return res; 15 deque<TreeNode*> deq; 16 deq.push_front(root);int flag = 0; 17 while(!deq.empty()){ 18 res.push_back(vector<int>()); 19 int n = deq.size(); 20 if(flag == 0){ //偶数层进,奇数层出 21 for(int i = 0;i < n;i ++){ 22 auto tmp = deq.back(); 23 deq.pop_back(); 24 res.back().push_back(tmp -> val); 25 if(tmp -> left) deq.push_front(tmp -> left); 26 if(tmp -> right) deq.push_front(tmp -> right); 27 } 28 flag = ! flag; 29 } 30 else{ //奇数层进,偶数层出 31 for(int i = 0;i < n;i ++){ 32 auto tmp = deq.front(); 33 deq.pop_front(); 34 res.back().push_back(tmp -> val); 35 if(tmp -> right) deq.push_back(tmp -> right); 36 if(tmp -> left) deq.push_back(tmp -> left); 37 } 38 flag = ! flag; 39 } 40 } 41 return res; 42 } 43 };

浙公网安备 33010602011771号