1. 程式人生 > >【LeetCode】【Python】二叉樹的最大深度

【LeetCode】【Python】二叉樹的最大深度

題目

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

二叉樹的深度為根節點到最遠葉子節點的最長路徑上的節點數。

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

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

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

方法:

二叉樹中開始運用迭代的方法,也有用在連結串列中也使用的佇列的方式

樹的遍歷分為深度優先和廣度優先,其中深度優先採用迭代的方式進行求解。

程式碼如下

# 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 maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root==None:
            return 0 
        else:
            return 1+max(self.maxDepth(root.left),self.maxDepth(root.right))

不斷的迭代直到葉子節點返回0,返回每個節點的最大深度就可以了。

廣度優先的遍歷要藉助佇列,通過佇列將

相關推薦

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

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

LeetCode深度(簡單

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

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

深度深度

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

深度(Java)

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

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 =

leetcode 104深度 & 111深度

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

leetcode 662寬度

一開始的思路是用bfs,但是感覺處理起來比較麻煩,後續會更新bfs的方法,這裡先貼上dfs的方法。 /**  * Definition for a binary tree node.  * struct TreeNode {  *     int val;  *     T

LeetCode 124. Binary Tree Maximum Path Sum(路徑和)

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node

leetcode 111-深度

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

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 124 Binary Tree Maximum Path Sum 路徑和

原題連結 題目描述 Given a binary tree, find the maximum path sum. 給出一棵二叉樹,計算其最大路徑和。 The path may start and end at any node in the t

CODEVS 1501寬度和高度

它的 logs nbsp 二叉 ace 最小寬度 最大 -h blog 題目描述 Description 給出一個二叉樹,輸出它的最大寬度和高度。 輸入描述 Input Description 第一行一個整數n。 下面n行每行有兩個數,對於第i行的兩個數

深度

als 最小 log root roo null mat dep tde 1.求二叉樹最大深度 public int maxDepth(TreeNode root) { if(root==null){ return 0;

18.2.14 codevs1501 寬度和高度

isp 連接 左右 ron esp color 整數 end codevs 題目描述 Description 給出一個二叉樹,輸出它的最大寬度和高度。 輸入描述 Input Description 第一行一個整數n。 下面n行每行有兩

2018.3.26 1501 寬度和高度

一個空格 post 12px 一行 個數 padding pac ide urn 題目描述 給出一個二叉樹,輸出它的最大寬度和高度。 輸入描述 第一行一個整數n。下面n行每行有兩個數,對於第i行的兩個數,代表編號為i的節點所連接的兩個左右兒子的編號。如果沒有某個兒子

演算法題系列之一 - 深度

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

LeetCode662 寬度

給定一個二叉樹,編寫一個函式來獲取這個樹的最大寬度。樹的寬度是所有層中的最大寬度。這個二叉樹與滿二叉樹(full binary tree)結構相同,但一些節點為空。 每一層的寬度被定義為兩個端點(該層最左和最右的非空節點,兩端點間的null節點也計入長度)之間的長度。 示例 1: 輸入: