1. 程式人生 > >Python, LeetCode, 111. 二叉樹的最小深度

Python, LeetCode, 111. 二叉樹的最小深度

# 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):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None:
            return 0
        leftDepth = self.minDepth(root.left)
        rightDepth = self.minDepth(root.right)
        if root.left is None or root.right is None:
            return max(leftDepth, rightDepth) + 1
        return min(leftDepth, rightDepth) + 1

相關推薦

leetcode 111-深度

最小深度定義為根節點到最近葉子節點的深度 分析: 空樹,最小深度為0 左右子樹都為空,最小深度為1 左右子樹不都為空,左右子樹中有空樹的情況,最小深度一定是在非空樹中產生,因為最小深度定義為到最

leetcode 104深度 & 111深度

def maxDepth(root): """ 非遞迴,用棧表示,stack棧儲存節點 """ if not root: return 0 count = 0 stack =

leetcode 104深度 & 111深度

def maxDepth(root): """ 非遞迴,用棧表示,stack棧儲存節點 """ if not root:

LeetCode深度(簡單

給定一個二叉樹,找出其最小深度。 最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \

Python, LeetCode, 111. 深度

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

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

LeetCode深度(簡單

問題描述: 給定一個二叉樹,找出其最大深度。 二叉樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20

演算法題系列之一 - 深度

題目: 給定一個二叉樹,找到它的最小深度,最小深度是從根節點到最近葉節點的最短路徑上的節點數。 答案:   public class Solution { public int run(TreeNode root) { if(root==null){

深度探究

一、求解最小深度 題目是這樣描述的:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root no

深度問題

【問題描述】: Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root

Leetcode111: Minimum Depth of Binary Tree(深度

先看Leetcode104:Maximum Depth of Binary Tree,求二叉樹最大深度。有兩種解法,基於DFS(深度優先搜尋)思想的方法使用遞迴計算:class Solution { public: int maxDepth(TreeNode *roo

Swift 求深度

給定一個二叉樹,找出其最小深度。 最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7],返回它的最小深度  2. 我們用遞迴方法求,求法和求最大深度類似,

資料結構---深度

【問題描述】:  Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node d

leetcode 111. 深度(python)

給定一個二叉樹,找出其最小深度。 最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \

[leetcode] 111. 深度

111. 二叉樹的最小深度 與獲取最大深度不同的是,要注意判斷下是不是葉子節點, class Solution { public int minDepth(TreeNode root) { if (root == null) { return 0;

Leetcode:111.深度

給定一個二叉樹,找出其最小深度。 最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \

leetcode 111. 深度(java)

給定一個二叉樹,找出其最小深度。 最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \

leetcode-111. 深度

題目 給定一個二叉樹,找出其最小深度。 最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20

LeetCode 111. 深度

111. 二叉樹的最小深度 題目描述提示幫助提交記錄社群討論閱讀解答 隨機一題 給定一個二叉樹,找出其最小深度。 最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,nu

LeetCode-111.深度(相關話題:深度優先)

給定一個二叉樹,找出其最小深度。 最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。 說明: 葉子節點是指沒有子節點的節點。 示例: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \