ZigZag Level Order Traversal BT
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
void helper(vector<vector<int>>& res, int level, TreeNode* node) { if(!node) return; if(res.size() == level) res.emplace_back(); res[level].push_back(node->val); helper(res,level + 1, node->left); helper(res,level + 1, node->right); } vector<vector<int>> Solution::zigzagLevelOrder(TreeNode* A) { vector<vector<int>> res; helper(res, 0, A); for(int i = 1; i < res.size(); i += 2) reverse(begin(res[i]), end(res[i])); return res; }
|