Binary Tree Level Order Traversal

我竟然先做的前一题,无语了

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> >res;
    void dfs(TreeNode *root,int level){
        if(!root) return;
        if(level == res.size()){//初始化vector
            res.push_back(vector<int>());
        }
        res[level].push_back(root->val);
        dfs(root->left,level+1);
        dfs(root->right,level+1);
    }
    vector<vector<int> > levelOrder(TreeNode *root) {
        dfs(root,0);
        return res;
    }
};

 

posted @ 2015-03-14 14:30  SprayT  阅读(97)  评论(0编辑  收藏  举报