[Swift]LeetCode104. 二叉樹的最大深度 | Maximum Depth of Binary Tree

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-tepirqyo-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a binary tree, find its maximum depth.node

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.git

Note: A leaf is a node with no children.github

Example:微信

Given binary tree [3,9,20,null,null,15,7],spa

    3
   / \
  9  20
    /  \
   15   7

return its depth = 3.code


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

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

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

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

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。


 32ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     func maxDepth(_ root: TreeNode?) -> Int {
16         //將樹分爲左子樹和右子樹兩部分,左右子樹中深度最大的即爲該樹結構的深度。
17         var max:Int = 0
18         if root != nil
19         {
20             max += 1
21             var maxLeft:Int = maxDepth(root!.left)
22             var maxRight:Int = maxDepth(root!.right)
23             max += (maxLeft > maxRight ? maxLeft : maxRight)
24         }
25         return max   
26     }
27 }

 24ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     
16     func maxDepth(_ root: TreeNode?) -> Int {
17         
18         guard let node = root else {
19             return 0
20         }
21         
22         return 1 + max(maxDepth(node.left), maxDepth(node.right))
23         
24     }
25 }

28ms

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     
16     func maxDepth(_ root: TreeNode?) -> Int {
17         
18         guard let node = root else {
19             return 0
20         }
21         
22         var depth: Int = 0
23         var stack: [TreeNode] = [node]
24         
25         while !stack.isEmpty {
26             
27             var currentStackSize = stack.count
28             
29             while currentStackSize > 0 {
30                 
31                 let lastNode = stack.popLast()!
32                 
33                 if let last = lastNode.left {
34                     stack.insert(last, at: 0)
35                 }
36                 
37                 if let right = lastNode.right {
38                     stack.insert(right, at: 0)
39                 }
40                 
41                 currentStackSize -= 1
42             }
43             
44             depth += 1
45         }
46         
47         return depth
48     }
49 }
相關文章
相關標籤/搜索