107.Maximum Depth of Binary Tree II

和104题目相似,区别在于本题树的层数由低到高存储。

107.Maximum Depth of Binary Tree II_第1张图片

代码:

class Solution {

public:

    vector> result;

void trval(TreeNode* t,int level)

{

    int maxlevel=result.size();

    if(maxlevel

    {

        vectortemp;

        temp.push_back(t->val);

        result.insert(result.begin(),temp);

    }

    else

    {

        result[maxlevel-level].push_back(t->val);

    }

    if(t->left!=NULL)

        trval(t->left, level+1);

    if(t->right!=NULL)

        trval(t->right, level+1);

}

vector> levelOrderBottom(TreeNode* root){

    if(root!=NULL)

        trval(root, 1);

    return result;

}

};

你可能感兴趣的:(107.Maximum Depth of Binary Tree II)