[Swift]LeetCode111. 二叉樹的最小深度 | Minimum Depth of Binary Tree

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

Given a binary tree, find its minimum depth.node

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest 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 minimum depth = 2.rest


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

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

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

示例:

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

    3
   / \
  9  20
    /  \
   15   7

返回它的最小深度  2.


 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 minDepth(_ root: TreeNode?) -> Int {
16         //深度優先搜索DFS
17         if root == nil {return 0}
18         if root!.left == nil && root!.right == nil
19         {
20             return 1
21         }
22         if root!.left == nil
23         {
24             return minDepth(root!.right) + 1
25         }
26         else if root!.right == nil
27         {
28             return minDepth(root!.left) + 1
29         }
30         else
31         {
32             let depthLeft:Int = minDepth(root!.left)
33             let depthRight:Int = minDepth(root!.right)
34             return (1 + ((depthLeft < depthRight) ? depthLeft : depthRight))
35         }
36     }
37 }

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 minDepth(_ root: TreeNode?) -> Int {
16         guard let root = root else { return 0 }
17         let left = minDepth(root.left)
18         let right = minDepth(root.right)
19         return 1 + ((left == 0 || right == 0) ? max(left, right) : min(left, right))
20     }
21 }

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     func minDepth(_ root: TreeNode?) -> Int {
16         guard let root = root else {
17             return 0
18         }
19         
20         let left = minDepth(root.left)
21         let right = minDepth(root.right)
22         
23         if left == 0 {
24             return right + 1
25         } else if right == 0 {
26             return left + 1
27         } else {
28             return min(left, right) + 1
29         }
30     }
31 }
相關文章
相關標籤/搜索