• [Swift]LeetCode110. 平衡二叉树 | Balanced Binary Tree


    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/9708855.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    Given a binary tree, determine if it is height-balanced.

    For this problem, a height-balanced binary tree is defined as:

    a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

    Example 1:

    Given the following tree [3,9,20,null,null,15,7]:

        3
       / 
      9  20
        /  
       15   7

    Return true.

    Example 2:

    Given the following tree [1,2,2,3,3,null,null,4,4]:

           1
          / 
         2   2
        / 
       3   3
      / 
     4   4
    

    Return false.


    给定一个二叉树,判断它是否是高度平衡的二叉树。

    本题中,一棵高度平衡二叉树定义为:

    一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。

    示例 1:

    给定二叉树 [3,9,20,null,null,15,7]

        3
       / 
      9  20
        /  
       15   7

    返回 true 。

    示例 2:

    给定二叉树 [1,2,2,3,3,null,null,4,4]

           1
          / 
         2   2
        / 
       3   3
      / 
     4   4
    

    返回 false 。


     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 isBalanced(_ root: TreeNode?) -> Bool {
    16         //递归分治法
    17         if root == nil {return true}
    18         
    19         var depthLeft:Int = depth(root?.left)
    20         var depthRight:Int = depth(root?.right)
    21         if abs(depthLeft-depthRight) > 1
    22         {
    23             return false
    24         }
    25         else
    26         {
    27             return isBalanced(root?.left) && isBalanced(root?.right)
    28         }
    29     }
    30     //求树的高度
    31     func depth(_ n:TreeNode?)->Int
    32     {
    33         if n==nil
    34         {
    35             return 0
    36         }
    37         else if n?.left == nil && n?.right == nil
    38         {
    39             return 1
    40         }
    41         else
    42         {
    43             var depthLeft:Int = depth(n?.left)
    44             var depthRight:Int = depth(n?.right)
    45             return (1 + ((depthLeft > depthRight) ? depthLeft : depthRight))
    46         }       
    47     }
    48 }

    48ms

     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 isBalanced(_ root: TreeNode?) -> Bool {
    17 //         guard let root = root else {
    18 //             return true
    19 //         }
    20 //         if abs(getDepth(root.left) - getDepth(root.right)) > 1 {
    21 //             return false
    22 //         }
    23 //         return isBalanced(root.left) && isBalanced(root.right)
    24 //     }
    25     
    26 //     func getDepth(_ root: TreeNode?) -> Int {
    27 //         if root == nil {
    28 //             return 0
    29 //         }
    30 //         return max(getDepth(root!.left), getDepth(root!.right)) + 1
    31 //     }
    32     
    33     func isBalanced(_ root: TreeNode?) -> Bool {
    34         if checkDepth(root) == -1 {
    35             return false
    36         }
    37         return true
    38     }
    39     
    40     func checkDepth(_ root: TreeNode?) -> Int {
    41         guard let root = root else {
    42             return 0
    43         }
    44         
    45         let left = checkDepth(root.left)
    46         if left == -1 {
    47             return -1
    48         }
    49         
    50         let right = checkDepth(root.right)
    51         if right == -1 {
    52             return -1
    53         }
    54         
    55         if abs(left - right) > 1 {
    56             return -1
    57         }
    58         
    59         return max(left, right) + 1
    60     }
    61 }

    52ms

     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 isBalanced(_ root: TreeNode?) -> Bool {
    16         var result = true
    17         isBalanced(root, &result)
    18         return result
    19     }
    20     
    21     func isBalanced(_ root: TreeNode?, _ result: inout Bool) -> Int {
    22         guard let root = root else {
    23             return 0
    24         }
    25         
    26         guard result else {
    27             return 0
    28         }
    29         
    30         let leftHeight = isBalanced(root.left, &result)
    31         let rightHeight = isBalanced(root.right, &result)
    32         if abs(leftHeight - rightHeight) > 1 {
    33             result = false
    34         }
    35         
    36         return 1 + max(leftHeight, rightHeight)
    37     }
    38 }

    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 isBalanced(_ root: TreeNode?) -> Bool {
    16         return root?.depth() != Int.max
    17     }
    18     
    19 }
    20 
    21 extension TreeNode {
    22     func depth(withPreviousDepth depth : Int = 1) -> Int {
    23         let leftDepth = self.left?.depth(withPreviousDepth: depth + 1) ?? depth
    24         let rightDepth = self.right?.depth(withPreviousDepth: depth + 1) ?? depth
    25         
    26         // looks repetitive, but it has better real-world
    27         // runtime performance than using max() and abs()
    28         if (leftDepth > rightDepth) {
    29             guard leftDepth - rightDepth <= 1 else { return Int.max }
    30             return leftDepth
    31         } else {
    32             guard rightDepth - leftDepth <= 1 else { return Int.max }
    33             return rightDepth
    34         }
    35     }
    36 }

    36ms

     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 isBalanced(_ root: TreeNode?) -> Bool {
    16         return dfs(root) != -1
    17     }
    18     
    19     func dfs(_ node: TreeNode?) -> Int {
    20       
    21         if node == nil {
    22             return 0
    23         }
    24         
    25         let leftHeight = dfs(node?.left)
    26         
    27         if leftHeight == -1 {
    28             return -1
    29         }
    30         
    31         let rightHeight = dfs(node?.right)
    32         
    33         if rightHeight == -1 {
    34             return -1
    35         }
    36         
    37         let totalHeight = abs(leftHeight - rightHeight)
    38         
    39         if totalHeight > 1 {
    40             return -1
    41         }
    42         
    43         return max(leftHeight, rightHeight) + 1
    44     }
    45     
    46 }
  • 相关阅读:
    3星|《葡萄酒经济学》:行业概况与资料汇编
    抖音、YouTube、Facebook等新媒体营销与运营相关14本书
    3星|《游戏学》:中美两国游戏产业概况
    3星|《新引爆点》:企业玩抖音入门
    4星|《未来的处方》:美国医疗组织应对奥巴马医改的成功经验12条
    2.5星|《知识付费》:行业相关资料的综述与堆砌
    《都挺好》原著小说大结局(严重剧透)
    OKR相关4本书,好书3本
    2.5星|《区块链超入门》:偏技术的介绍,没介绍过去两年间币圈的各种或狗血或精彩的故事与事故
    《经济学人》电子书15本,大部分水平较高
  • 原文地址:https://www.cnblogs.com/strengthen/p/9708855.html
Copyright © 2020-2023  润新知