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

来源:互联网 发布:淘宝客和返利网是什么 编辑:程序博客网 时间:2024/04/28 08:21

练习了一下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;    }};