Offer surface prove safety questions. Zigzag order of the print binary

Title Description

Implement according to a zigzag print function binary tree, i.e., left to right, the first print line of the second layer in order to print from right to left, the third line from left to right order of printing, in other rows forth.
 

method 1:

Normal traverse the level, using common queue. Every odd line (counting from 0) put the result of the reverse layer.

 1 class Solution {
 2 public:
 3     vector<vector<int> > Print(TreeNode* pRoot) {
 4         if(pRoot==nullptr){return {};}
 5         queue<TreeNode*> my_queue;
 6         my_queue.push(pRoot);
 7         vector<vector<int>>res;
 8         vector<int> vec;
 9         int cnt=0;
10         while(not my_queue.empty()){
11             vec.clear();
12             int cur_siz=my_queue.size();
13             for(int i=0;i<cur_siz;++i){
14                 auto cur=my_queue.front();
15                 my_queue.pop();
16                 vec.push_back(cur->val);
17                 if(cur->left){
18                     my_queue.push(cur->left);
19                 }
20                 if(cur->right){
21                     my_queue.push(cur->right);
22                 }
23             }
24             if(cnt&1){
25                 reverse(vec.begin(),vec.end());
26             }
27             res.push_back(vec);
28             ++cnt;
29         }
30         return res;
31     }
32 };

 

Method 2:

Using deque, the queue is always normal order, but the odd rows from right to left traverse, the right pop, push into the left child. Even lines from left to right traverse, pop left, the children push into right.

 1 class Solution {
 2 public:
 3     vector<vector<int> > Print(TreeNode* pRoot) {
 4         if(pRoot==nullptr){return {};}
 5         deque<TreeNode*> my_queue;
 6         my_queue.push_back(pRoot);
 7         vector<vector<int>>res;
 8         int cnt=0;
 9         while(not my_queue.empty()){
10             res.push_back({});
11             int cur_siz=my_queue.size();
12             decltype(pRoot) cur;
13             for(int i=0;i<cur_siz;++i){
14                 if(cnt&1){
15                     cur=my_queue.back();
16                     my_queue.pop_back();
17                     if(cur->right){
18                         my_queue.push_front(cur->right);
19                     }
20                     if(cur->left){
21                         my_queue.push_front(cur->left);
22                     }
23                 }
24                 else{
25                     cur=my_queue.front();
26                     my_queue.pop_front();
27                     if(cur->left){
28                         my_queue.push_back(cur->left);
29                     }
30                     if(cur->right){
31                         my_queue.push_back(cur->right);
32                     }
33                 }
34                 res.back().push_back(cur->val);
35              }
 36              ++ cnt;
37          }
 38          return anything;
39      }
 40 };

 

Guess you like

Origin www.cnblogs.com/FdWzy/p/12306131.html