[LeetCode]111. Minimum Depth of Binary Tree★

题目描述

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Note: A leaf is a node with no children.
题目大意:给定一个二叉树,求到二叉树叶子节点的最小深度

样例

Example 1:

Given binary tree [3,9,20,null,null,15,7],
二叉树
return its minimum depth = 2.

Example 2:

Given binary tree [1,2],
二叉树
return its minimum depth = 2.

python解法

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def minDepth(self, root: TreeNode) -> int:
        import queue
        q = queue.Queue()
        if not root:
            return 0
        q.put(root)
        depth = 1 # 树深
        hp = 0 # 记录已访问节点的个数
        tp = 1 # 记录进入队列的节点个数
        count = 1 # 判断是否已经访问到每一层的最后一个节点
        while not q.empty():
            p = q.get()
            hp += 1
            if not p.left and not p.right:
                break
            if p.left:
                q.put(p.left)
                tp += 1
            if p.right:
                q.put(p.right)
                tp += 1
            if hp == count:
                depth += 1
                count = tp
        return depth

Runtime: 52 ms, faster than 65.17% of Python3 online submissions for Minimum Depth of Binary Tree.
Memory Usage: 15.3 MB, less than 62.16% of Python3 online submissions for Minimum Depth of Binary Tree.
题后反思:

  1. python代码中尽量少些全局变量,尽量将变量都定义在类内或方法内。

C语言解法

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

struct Queue {
    struct TreeNode * node;
    struct Queue *next;
};

struct Queue * initQueue()
{
    struct Queue *head = (struct Queue *)malloc(sizeof(struct Queue));
    head -> next = NULL;
    head -> node = NULL;
    return head;
}

bool empty(struct Queue *head)
{
    return head -> next ? false : true;
}

void EnQueue(struct Queue *head, struct TreeNode * node)
{
    struct Queue *p = head;
    while(p -> next)
    {
        p = p -> next;
    }
    p -> next = (struct Queue *)malloc(sizeof(struct Queue));
    p -> next -> next = NULL;
    p -> next -> node = node;
}

struct TreeNode*DeQueue(struct Queue *head)
{
    struct Queue *p = head -> next;
    head -> next = p -> next;
    struct TreeNode*r = p -> node;
    free(p);
    return r;
}

void clear(struct Queue *head)
{
    struct Queue *p = head -> next, *q = NULL;
    while(p)
    {
        q = p;
        p = p -> next;
        free(q);
    }
    free(head);
}

int minDepth(struct TreeNode* root){
    if (!root)
    {
        return 0;
    }
    struct Queue *head = initQueue();
    struct TreeNode *p = NULL;
    EnQueue(head, root);
    int hp = 0, tp = 1, count = 1, depth = 1;
    while(!empty(head))
    {
        p = DeQueue(head);
        hp ++;
        if (!p -> left && !p -> right)
        {
            break;
        }
        if (p -> left)
        {
            EnQueue(head, p -> left);
            tp ++;
        }
        if (p -> right)
        {
            EnQueue(head, p -> right);
            tp ++;
        }
        if (hp == count)
        {
            depth ++;
            count = tp;
        }
    }
    clear(head);
    return depth;
}

Runtime: 8 ms, faster than 72.46% of C online submissions for Minimum Depth of Binary Tree.
Memory Usage: 10 MB, less than 100.00% of C online submissions for Minimum Depth of Binary Tree.
题后反思:

  1. 用层次遍历二叉树的思想求最小深度,当第一次访问到一个既没有左孩子也没有右孩子的节点时,直接终止循环,并将记录的深度返回。
  2. 之前看过书里面的一个算法,用求二叉树深度的递归算法求到达叶子节点的最小深度
    算法如下:
void MaxMinLeaf(BiTree T,int *max,int *min) 
{ 
    int m1,m2,n1,n2; 
    if(T==NULL) 
    { 
        *max=0;
        *min=0; 
     } 
     else 
     {
         MaxMinLeaf(T->lchild,m1,n1);
         MaxMinLeaf(T->rchild,m2,n2);        
         *max=(m1>m2?m1:m2)+1; 
         *min=(m1<m2?m1,m2)+1;  
     }
}

但是我觉得是不对的,例如样例2,但是我也没想到比较好的改法。。。
文中都是我个人的理解,如有错误的地方欢迎下方评论告诉我,我及时更正,大家共同进步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值