美文网首页
107.Maximum Depth of Binary Tree

107.Maximum Depth of Binary Tree

作者: 花落花开花满天 | 来源:发表于2018-11-15 19:27 被阅读0次

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

    代码:

    class Solution {

    public:

        vector<vector<int>> result;

    void trval(TreeNode* t,int level)

    {

        int maxlevel=result.size();

        if(maxlevel<level)

        {

            vector<int>temp;

            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<vector<int>> levelOrderBottom(TreeNode* root){

        if(root!=NULL)

            trval(root, 1);

        return result;

    }

    };

    相关文章

      网友评论

          本文标题:107.Maximum Depth of Binary Tree

          本文链接:https://www.haomeiwen.com/subject/oekufqtx.html