Binary Tree Level Order Traversal II

问题:输出二叉树的每一行的结点,从叶子到根

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int dfs(TreeNode *root)
    {
        if(root==NULL) return 0;
        if(root->left==NULL && root->right==NULL) return 1;
        return max(dfs(root->left),dfs(root->right))+1;
    }
    void Ddfs(TreeNode *root,int t,vector<int> &vec,int step)
    {
        if(step==t) vec.push_back(root->val);
        if(root->left)  Ddfs(root->left,t,vec,step+1);
        if(root->right) Ddfs(root->right,t,vec,step+1);
    }
    vector<vector<int> > levelOrderBottom(TreeNode *root) {
        int height=dfs(root);
        vector<vector<int> > vec1;
        for(int i=height;i>=1;i--)
        {
            vector<int> vec2;
            Ddfs(root,i,vec2,1);
            vec1.push_back(vec2);
        }
        return vec1;
    }
};

  

posted @ 2014-08-06 17:57  calmound  阅读(133)  评论(0编辑  收藏  举报