题解 | #旋转数组的最小数字#
按之字形顺序打印二叉树
http://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0
使用deque, 双端队列
class Solution { public: vector<vector<int>> Print(TreeNode *pRoot) { if (!pRoot) { return {}; } vector<vector<int>> res; vector<int> resT; deque<TreeNode *> visit; visit.emplace_back(pRoot); int size = 1; bool flag = 1; while (!visit.empty()) { size--; if (flag) { auto now = visit.back(); visit.pop_back(); resT.emplace_back(now->val); if (now->left != nullptr) { visit.emplace_front(now->left); } if (now->right != nullptr) { visit.emplace_front(now->right); } } else { auto now = visit.front(); visit.pop_front(); resT.emplace_back(now->val); if (now->right != nullptr) { visit.emplace_back(now->right); } if (now->left != nullptr) { visit.emplace_back(now->left); } } if (size == 0) { flag = !flag; size = visit.size(); res.emplace_back(move(resT)); resT = vector<int>(); } } return res; } };