LeetCode103 二叉树的锯齿形层次遍历

给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。

其实就是一个栈,对于奇数层先压入左子树,对于偶数层先压入右子树。但是每一层需要先把所有节点都取出来才能开始循环。

 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 private:
12     stack<TreeNode*> s;
13 public:
14     vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
15         if(root==nullptr)
16             return {};
17         s.push(root);
18         int layer=1;
19         vector<vector<int>> ans;
20         while(!s.empty()){
21             int len=s.size();
22             vector<int> tempans;
23             queue<TreeNode*> curlayer;
24             while(len){
25                 --len;
26                 curlayer.push(s.top());
27                 s.pop();
28             }
29             while(!curlayer.empty()){
30                 TreeNode* temp=curlayer.front();
31                 curlayer.pop();
32                 tempans.push_back(temp->val);
33                 if(layer&1==1){
34                     if(temp->left!=nullptr)
35                         s.push(temp->left);
36                     if(temp->right!=nullptr)
37                         s.push(temp->right);
38                 }
39                 else{
40                     if(temp->right!=nullptr)
41                         s.push(temp->right);
42                     if(temp->left!=nullptr)
43                         s.push(temp->left);
44                 }
45             }
46             ++layer;
47             ans.push_back(tempans);
48         }
49         return ans;
50     }
51 };

看别人的答案很机智,其实直接一个queue按层次存取节点;然后一个每一层用一个deque存储,奇数层就push_back,偶数层就push_front。

 1 class Solution {
 2 public:
 3     vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
 4         vector<vector<int>> res;
 5         
 6         if (root == NULL)   return {};  
 7         queue<TreeNode*> q;     
 8         q.push(root);  
 9 
10         int level=0;
11         while (!q.empty()) 
12         {  
13             vector<int>temp;                //存放每一层的元素值
14             int count=q.size();             //队列大小表示当前层数的元素个数
15             while(count--)                  //count--逐个对该层元素进行处理
16             {
17             TreeNode *t=q.front();             q.pop();    
18             if(level%2 ==0) 
19             temp.push_back(t->val);
20             else
21             temp.insert(temp.begin(),t->val);
22             if(t->left)     q.push(t->left);
23             if(t->right)    q.push(t->right);
24             }
25 
26             level++;
27             res.push_back(temp);           //将当层元素的vector加入res中
28         }
29         return res;
30         }
31 };
32 
33 
34 
35 作者:zrita
36 链接:https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/solution/c-2chong-fang-fa-dfsbfsdi-gui-he-dui-lie-jian-ji-y/
37 来源:力扣(LeetCode)
38 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自www.cnblogs.com/rookiez/p/13388580.html