您的位置:首页 > 其它

[leetcode刷题系列]Binary Tree Level Order Traversal II

2013-08-10 23:27 375 查看
练习了一下vector的resize用法- -

/**
* Definition for binary tree
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
vector<vector<int> > vc;
void dfs(TreeNode * root, int dep){
if(vc.size() < dep)
vc.resize(dep);
vc[dep - 1].push_back(root->val);
if(root->left != 0)
dfs(root->left, dep + 1);
if(root->right != 0)
dfs(root->right, dep + 1);
}
public:
vector<vector<int> > levelOrderBottom(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
// reset
vc.clear();
if(root == 0)
return vc;
// get the ans
dfs(root, 1);
reverse(vc.begin(), vc.end());
// ret
return vc;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: