LeetCode104\111 二叉树最大/最小深度

原题目

给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ ``\
9 `20
/``````\
15 ```7
返回它的最大深度 3 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree

第二题

给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/ \
9 20
/ ```\
15 7
返回它的最小深度 2.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-depth-of-binary-tree

题目分析
第一题

方法一:递归,从底至上
方法二:dfs,前序遍历
方法三:深搜,层次遍历
方法二和方法三都一样,记录每个节点的层次

第二题

同第一题

完整代码
第一题

方法一

int maxDepth(struct TreeNode* root){
    if(root==NULL)return 0;
    int rDepth=maxDepth(root->left);
    int lDepth=maxDepth(root->right);
    return fmax(rDepth,lDepth)+1;
}

方法二:

struct Stack
{
    struct TreeNode *node;
    int depth;
};

int maxDepth(struct TreeNode* root){
    if(root==NULL)return 0;
    int max_Depth=1;
    struct Stack stack[1000];
    int top=0;
    stack[top].node=root;
    stack[top].depth=1;
    top++;
    while(top)
    {
        root=stack[top-1].node;
        int cur_Depth=stack[top-1].depth;
        top--;
        if(root->left)
        {
            stack[top].node=root->left;
            stack[top].depth=cur_Depth+1;
            top++;
        }
        if(root->right)
        {
            stack[top].node=root->right;
            stack[top].depth=cur_Depth+1;
            top++;
        }
        if(max_Depth<cur_Depth)max_Depth=cur_Depth;
    }
    return max_Depth;
}

方法三:

struct Queue
{
    struct TreeNode *node;
    int depth;
};

int maxDepth(struct TreeNode* root){
    if(root==NULL)return 0;
    int max_Depth=1;
    struct Queue queue[500];
    int front=0,rear=0;
    queue[rear].node=root;
    queue[rear].depth=1;
    rear++;
    while(front!=rear)
    {
        root=queue[front].node;
        int cur_Depth=queue[front].depth;
        front=(front+1)%500;
        if(root->left)
        {
            queue[rear].node=root->left;
            queue[rear].depth=cur_Depth+1;
            rear=(rear+1)%500;
        }
        if(root->right)
        {
            queue[rear].node=root->right;
            queue[rear].depth=cur_Depth+1;
            rear=(rear+1)%500;
        }
        if(max_Depth<cur_Depth)max_Depth=cur_Depth;
    }
    return max_Depth;
}
第二题

方法一:

int minDepth(struct TreeNode* root){
    if(root==NULL)return 0;
    if(root->left==NULL&&root->right==NULL)return 1;
    int lDepth=INT_MAX,rDepth=INT_MAX;
    if(root->left)lDepth=minDepth(root->left);
    if(root->right)rDepth=minDepth(root->right);
    return fmin(lDepth,rDepth)+1;
}

方法二:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

struct Stack
{
    struct TreeNode *node;
    int depth;
};

int minDepth(struct TreeNode* root){
    if(root==NULL)return 0;
    int max_Depth=INT_MAX;
    struct Stack stack[1000];
    int top=0;
    stack[top].node=root;
    stack[top].depth=1;
    top++;
    while(top)
    {
        root=stack[top-1].node;
        int cur_Depth=stack[top-1].depth;
        top--;
        if(root->left)
        {
            stack[top].node=root->left;
            stack[top].depth=cur_Depth+1;
            top++;
        }
        if(root->right)
        {
            stack[top].node=root->right;
            stack[top].depth=cur_Depth+1;
            top++;
        }
        if(root->left==NULL&&root->right==NULL&&max_Depth>cur_Depth)
            max_Depth=cur_Depth;
    }
    return max_Depth;
}

方法三:

struct Queue
{
    struct TreeNode *node;
    int depth;
};

int minDepth(struct TreeNode* root){
    if(root==NULL)return 0;
    int max_Depth=INT_MAX;
    struct Queue queue[500];
    int front=0,rear=0;
    queue[rear].node=root;
    queue[rear].depth=1;
    rear++;
    while(front!=rear)
    {
        root=queue[front].node;
        int cur_Depth=queue[front].depth;
        front=(front+1)%500;
        if(root->left)
        {
            queue[rear].node=root->left;
            queue[rear].depth=cur_Depth+1;
            rear=(rear+1)%500;
        }
        if(root->right)
        {
            queue[rear].node=root->right;
            queue[rear].depth=cur_Depth+1;
            rear=(rear+1)%500;
        }
        if(root->left==NULL&&root->right==NULL&&max_Depth>cur_Depth)max_Depth=cur_Depth;
    }
    return max_Depth;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Baal Austin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值