Leetcode#103 Binary Tree Zigzag Level Order Traversal

原题地址

基本数据结构操作,二叉树的层次遍历。

代码:

 vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<vector<int> > res;
vector<TreeNode *> layer;
bool l2r = true; layer.push_back(root);
while (!layer.empty()) {
vector<TreeNode *> nextLayer;
vector<int> path;
for (auto node : layer) {
if (!node)
continue;
path.push_back(node->val);
nextLayer.push_back(node->left);
nextLayer.push_back(node->right);
}
if (!l2r) {
for (int i = ; (i << ) < path.size(); i++)
swap(path[i], path[path.size() - i - ]);
}
if (!path.empty())
res.push_back(path);
layer = nextLayer;
l2r = !l2r;
} return res;
}
上一篇:Python之在函数中使用列表作为默认参数


下一篇:上下问语句句柄Release地方