博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode – Refresh – Binary Tree Zigzag Level Order Traversal
阅读量:7220 次
发布时间:2019-06-29

本文共 1307 字,大约阅读时间需要 4 分钟。

Made a stupid bug....... When reverse the vector, stop it at mid. Otherwise, it will swap back......

Mark!!!!!!!!

1 /** 2  * Definition for binary tree 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
> zigzagLevelOrder(TreeNode *root) {13 vector
> result;14 vector
level;15 if (!root) return result;16 int current = 1, future = 0;17 bool flag = false;18 queue
q;19 q.push(root);20 auto func = [](int &a, int &b) { int t = a; a = b; b = t;};21 while (!q.empty()) {22 TreeNode *tmp = q.front();23 q.pop(), current--;24 if (tmp->left) {25 q.push(tmp->left);26 future++;27 }28 if (tmp->right) {29 q.push(tmp->right);30 future++;31 }32 level.push_back(tmp->val);33 if (!current) {34 if (flag) {35 for (int i = 0; i < level.size()/2; i++) {36 func(level[i], level[level.size()-i-1]);37 }38 }39 flag ^= 1;40 result.push_back(level);41 level.clear();42 current = future;43 future = 0;44 }45 }46 return result;47 }48 };

 

转载于:https://www.cnblogs.com/shuashuashua/p/4346248.html

你可能感兴趣的文章
OC语言BLOCK和协议
查看>>
C++创建一个动态链接库工程
查看>>
(六)maven之本地仓库
查看>>
如何使用 SPICE client (virt-viewer) 来连接远程虚拟机桌面?
查看>>
CentOS7
查看>>
linux高编IO-------tmpnam和tmpfile临时文件
查看>>
微信的机器人开发
查看>>
从零开始学Java(二)基础概念——什么是"面向对象编程"?
查看>>
近期面试总结(2016.10)
查看>>
CodeForces 525D Arthur and Walls :只包含点和星的矩阵,需要将部分星变成点使满足点组成矩形 : dfs+思维...
查看>>
积累_前辈的推荐
查看>>
strcpy和memcpy的区别《转载》
查看>>
在windows平台下electron-builder实现前端程序的打包与自动更新
查看>>
DroidPilot V2.1 手写功能特别版
查看>>
COOKIE欺骗
查看>>
js 强转规范解读
查看>>
ACdream - 1735:输油管道
查看>>
golang 获取get参数
查看>>
服务器状态码
查看>>
非小型电子商务系统设计经验分享
查看>>