Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - /**
 - * Definition for a binary tree node.
 - * struct TreeNode {
 - * int val;
 - * TreeNode *left;
 - * TreeNode *right;
 - * TreeNode() : val(0), left(nullptr), right(nullptr) {}
 - * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 - * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 - * };
 - */
 - class Solution {
 - public:
 - vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
 - if(!root) return {};
 - queue<TreeNode*> q;
 - vector< vector<int> > res;
 - q.push(root);
 - int level = 0;
 - while(!q.empty()){
 - int sz = q.size();
 - vector<int> curr(sz);
 - for(int i=0; i<sz; i++){
 - TreeNode *front = q.front();
 - q.pop();
 - if(level) curr[sz-i-1] = front->val;
 - else curr[i] = front->val;
 - if(front->left) q.push(front->left);
 - if(front->right) q.push(front->right);
 - }
 - res.push_back(curr);
 - level = !level;
 - }
 - return res;
 - }
 - };
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment