【力扣-二叉树】5、二叉树的深度(最大104、最小111)

「这是我参与11月更文挑战的第13天,活动详情查看:2021最后一次更文挑战

104. 二叉树的最大深度

题目描述

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7]

    3
   / \
  9  20
    /  \
   15   7
复制代码

返回它的最大深度 3 。

解析

最大深度,即找出根节点到叶子节点的最大路径中节点的个数

递归法

struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int v):val(v),left(NULL),right(NULL){};
};
class Solution{
public:
    int maxDepth(TreeNode *root){
        return getDepth(root);
    }
    
    int getDepth(TreeNode *root){
        if(root == NULL){
            return 0;
        }
        // 求左子树的深度
        int leftDepth = getDepth(root->left);
        // 求右子树的深度
        int rightDepth = getDepth(root->right);
        // 选择最大的深度,再加上根节点的深度1,即为树的深度
        int depth = max(leftDepth,rightDepth)+1;
        return depth;
    }
}
复制代码

迭代法

层序遍历的过程中,判断树的深度

struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int v):val(v),left(NULL),right(NULL){};
};

class Solution{
    public:
        int maxDepth(TreeNode *root){
            if(root == NULL){
                return 0;
            }
            // 树的最大深度
            int depth=0;
            
            queue<TreeNode *> que;
            que.push(root);
            while(!que.empty()){
                depth++; // 树的深度+1
                int size = que.size();
                // 遍历每一层的节点
                for(int i=0;i<size;i++){
                    TreeNode *node = que.front();
                    que.pop();
                    // 左子树
                    if(node->left){
                        que.push(node->left);
                    }
                    // 右子树
                    if(node=>right){
                        que.push(node->right);
                    }
                }
            }
            return depth;
        }
};
复制代码

111. 二叉树的最小深度

题目描述

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例 1:

输入: root = [3,9,20,null,null,15,7]
输出: 2
复制代码

示例 2:

输入: root = [2,null,3,null,4,null,5,null,6]
输出: 5
复制代码

解析

最小深度,即找出根节点到达叶子节点的最短路径中节点的数量

递归法

class Solution{
public:
    int minDepth(TreeNode *root){
        return getDepth(root);
    }
    int getDepth(TreeNode *root){
        if(root ==NULL){
            return 0;
        }
        // 左子树的深度
        int leftDepth = getDepth(root->left);
        // 右子树的深度
        int rightDepth = getDepth(root->right);
        // 如果左子树为空,右子树非空,返回右子树的深度
        if(root->left ==NULL && root->right!=NULL){
            return 1+rightDepth;
        }
        // 如果右子树为空,左子树非空,返回左子树的深度
        if(root->right == NULL && root->left!=NULL){
            return 1+leftDepth;
        }
        
        // 如果左右子树都为空,返回其中最小的
        int minDepth = min(leftDepth,rightDepth)+1;
        return minDepth;
    }
};
复制代码

image.png

迭代法

class Solution
{
public:
    int minDepth(TreeNode *root)
    {
        if (root == NULL)
        {
            return 0;
        }
        int depth = 0;
        queue<TreeNode *> que;
        que.push(root);

        while (!que.empty())
        {
            int size = que.size();
            
            depth++;
            for (int i = 0; i < size; i++)
            {
                TreeNode *node = que.front();
                que.pop();
                if(node->left){
                    que.push(node->left);
                }
                if(node->right){
                    que.push(node->right);
                }
                if(!node->left && !node->right){
                    return depth;
                }
            }
        }
        return depth;
    }
};
复制代码

image.png