107.Binary Tree Level Order Traversal ii
拆分任务:
1.层次遍历
2.翻转vector
class LevelOrderBottom {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
if (root == NULL)
{
return vector<vector<int>>();
}
vector<int> lineRes;
vector<vector<int>> res;
queue<TreeNode*> line;
int lineNum = 1;
int recordNum = 0;
line.push(root);
while(!line.empty())
{
TreeNode* node = line.front();
lineRes.push_back(node->val);
lineNum--;
if (node->left != NULL)
{
line.push(node->left);
recordNum++;
}
if (node->right != NULL)
{
line.push(node->right);
recordNum++;
}
line.pop();
if (lineNum == 0)
{
lineNum = recordNum;
recordNum = 0;
res.push_back(lineRes);
lineRes.clear();
}
}
reverse(res.begin(), res.end());
return res;
}
};