zoukankan      html  css  js  c++  java
  • Leecode刷题之旅-C语言/python-111二叉树的最小深度

    /*
     * @lc app=leetcode.cn id=111 lang=c
     *
     * [111] 二叉树的最小深度
     *
     * https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/
     *
     * algorithms
     * Easy (37.27%)
     * Total Accepted:    12.2K
     * Total Submissions: 32.6K
     * Testcase Example:  '[3,9,20,null,null,15,7]'
     *
     * 给定一个二叉树,找出其最小深度。
     * 
     * 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
     * 
     * 说明: 叶子节点是指没有子节点的节点。
     * 
     * 示例:
     * 
     * 给定二叉树 [3,9,20,null,null,15,7],
     * 
     * ⁠   3
     * ⁠  / 
     * ⁠ 9  20
     * ⁠   /  
     * ⁠  15   7
     * 
     * 返回它的最小深度  2.
     * 
     */
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    int min(a,b);
    int minDepth(struct TreeNode* root) {
            if (root == NULL) return 0;
            if (root->left == NULL && root->right == NULL) return 1;
            if (root->left == NULL) return minDepth(root->right) + 1;
            else if (root->right == NULL) return minDepth(root->left) + 1;
            else return 1 + min(minDepth(root->left), minDepth(root->right));
    }
    int min(a,b){
        return a<b?a:b;
    }

    最小深度和最大深度类似,但是要注意的就是,当左子树为空的时候,只查右子树就可以,右子树为空的时候,只查左子树即可。

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    python:

    #
    # @lc app=leetcode.cn id=111 lang=python3
    #
    # [111] 二叉树的最小深度
    #
    # https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/
    #
    # algorithms
    # Easy (37.27%)
    # Total Accepted:    12.2K
    # Total Submissions: 32.6K
    # Testcase Example:  '[3,9,20,null,null,15,7]'
    #
    # 给定一个二叉树,找出其最小深度。
    # 
    # 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
    # 
    # 说明: 叶子节点是指没有子节点的节点。
    # 
    # 示例:
    # 
    # 给定二叉树 [3,9,20,null,null,15,7],
    # 
    # ⁠   3
    # ⁠  / 
    # ⁠ 9  20
    # ⁠   /  
    # ⁠  15   7
    # 
    # 返回它的最小深度  2.
    # 
    #
    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    
    # 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):
            if root == None:
                return 0
            elif root.left == None and root.right == None:
                return 1
            elif root.left == None and root.right != None:
                return self.minDepth(root.right)+1
            elif root.right ==None and root.left != None:
                return self.minDepth(root.left)+1
            elif root.left != None and root.right !=None:
                return min(self.minDepth(root.left), self.minDepth(root.right))+1
  • 相关阅读:
    Windows 平台下的Mysql集群主从复制
    IOS 的loadView 及使用loadView中初始化View注意的问题。(死循环并不可怕)
    【2013625】K2+SAP集成应用解决方案在线研讨会
    to_char 和 to_date 经验分享
    Java向Access插入数据
    spring Bean的生命周期管理
    [置顶] 30分钟,让你成为一个更好的程序员
    Spring框架中Bean的生命周期
    Box2D的相关知识
    第八周项目二
  • 原文地址:https://www.cnblogs.com/lixiaoyao123/p/10523738.html
Copyright © 2011-2022 走看看