文章目录
层序模板
void traverse(TreeNode* root) {
if (!root)
return ;
//以root初始化一层
queue<TreeNode*> oneRow({root});
while (!oneRow.empty()) {
//访问当前层
int n = oneRow.size();
for (int i = 0; i < n; i++) {
TreeNode* cur = oneRow.front();
if (cur->left)
oneRow.push(cur->left);
if (cur->right)
oneRow.push(cur->right);
oneRow.pop();
}
}
}
题目顺序
01 102. 二叉树的层序遍历
02 429. N 叉树的层序遍历
03 107. 二叉树的层序遍历 II
reverse可直接反转vector