(层次遍历)104. 二叉树的最大深度

原题链接:(层次遍历)104. 二叉树的最大深度

思路:
使用层序遍历模板,遍历每一层 hight+1 返回hight即可

全代码:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> que;
        int hight = 0;
        if(root == NULL) return 0;
        que.push(root);
        while(!que.empty())
        {
            int size = que.size();
            for(int i = 0; i < size; i++)
            {
                TreeNode* cur = que.front();
                que.pop();
                if(cur ->left) que.push(cur ->left);
                if(cur ->right) que.push(cur ->right);
            }
            hight++;
        }
        return hight;
    }
};

你可能感兴趣的:(二叉树,数据结构)