1. 程式人生 > >Python刷leetcode111. 二叉樹的最小深度

Python刷leetcode111. 二叉樹的最小深度

給定一個二叉樹,找出其最小深度。

最小深度是從根節點到最近葉子節點的最短路徑上的節點數量。

說明: 葉子節點是指沒有子節點的節點。

示例:

給定二叉樹 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回它的最小深度  2.

思路:基本思路和求最大深度類似,區別是如果只有一個子結點不為空,則最小深度應取那個不為子結點的深度+1

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

class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        left = self.minDepth(root.left)
        right = self.minDepth(root.right)
        if left ==0 and right != 0:
            return right+1
        if left !=0 and right ==0:
            return left+1
        return min(left,right)+1

相關推薦

Pythonleetcode111. 深度

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

Leetcode111: Minimum Depth of Binary Tree(深度

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

LeetCode之深度(簡單

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

leetcode 104深度 & 111深度

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

演算法題系列之一 - 深度

題目: 給定一個二叉樹,找到它的最小深度,最小深度是從根節點到最近葉節點的最短路徑上的節點數。 答案:   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

leetcode 104深度 & 111深度

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

深度問題

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

leetcode 111-深度

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

Swift 求深度

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

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

資料結構---深度

【問題描述】:  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

深度深度

str treenode oot null 避免 結果 一個 blog clas 最大深度: int maxDepth(TreeNode *root) { if(root == NULL) return 0;

深度(遞迴實現python)---LeetCode

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

Pythonleetcode107. 的層次遍歷 II

給定一個二叉樹,返回其節點值自底向上的層次遍歷。 (即按從葉子節點所在層到根節點所在的層,逐層從左向右遍歷)例如:給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其自底向上的層次遍

LeetCode之深度(簡單

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

深度(Java)

題目:輸入一棵二叉樹的根節點,求該樹的深度。從根節點到葉子結點一次經過的結點形成樹的一條路徑,最長路徑的長度為樹的深度。根節點的深度為1。 解體思路: 如果根節點為空,則深度為0,返回0,遞迴的出口 如果根節點不為空,那麼深度至少為1,然後我們求他們左右子樹的深度, 比較左

leetCode 104.Maximum Depth of Binary Tree(深度) 解題思路和方法

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down t

LeetCode111 | 深度

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

leetcode111 深度 python

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