题目:
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res;
if(root == NULL)
return res;
int next_level = 0, cur_level = 1;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
vector<int> leaves;
while(cur_level--)
{
TreeNode* cur = q.front();
q.pop();
leaves.push_back(cur->val);
if(cur->left != NULL)
{
q.push(cur->left);
next_level++;
}
if(cur->right != NULL)
{
q.push(cur->right);
next_level++;
}
}
res.push_back(leaves);
cur_level = next_level;
next_level = 0;
}
reverse(res.begin(), res.end());
return res;
}
};
Easy难度的题,一次AC~
转眼就要2020年了,说来2020年真的很好出题啊,叠词的年份,期待~
感谢2019年,希望新的一年可以认真多写些有干货的博客 (ง •_•)ง
回顾2019年,关于coding部分,还是有几个关键的节点的,在年初参加的女子编程赛,凌晨三点交卷,五点半起床赶飞机= =
最后提交16秒的时候AC了一道题目,真的相当刺激了😍尽管最后还是酱油~
第二个节点就是遍布在上半年的coding,虽然最后结果也不是那么理想,但是人生就在于尝试啊!
从六月之后coding的次数就明显减少了,主要转战于paper。。。虽然结果也不那么。。。
好吧,看来今年就没什么显著性成果,但自己还算开心的一年。
新的一年,加油呀!
再见10年代,你好2020 ^______________________________^