Binary Tree Level Order Traversal
/**
* 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;
dfs(root, 0, res);
reverse(res.begin(), res.end());
return res;
}
private:
void dfs(TreeNode *r, int level, vector<vector<int>> &ret){
if(r == NULL) return;
if(level == ret.size())
ret.push_back(vector<int>());
ret[level].push_back(r->val);
dfs(r->left, level + 1, ret);
dfs(r->right, level + 1, ret);
}
};