• [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes


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

    Given a complete binary tree, count the number of nodes.

    Note:

    Definition of a complete binary tree from Wikipedia:
    In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

    Example:

    Input: 
        1
       / 
      2   3
     /   /
    4  5 6
    
    Output: 6

    给出一个完全二叉树,求出该树的节点个数。

    说明:

    完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

    示例:

    输入: 
        1
       / 
      2   3
     /   /
    4  5 6
    
    输出: 6

    96ms
     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 countNodes(_ root: TreeNode?) -> Int {
    16         guard  root != nil else{
    17             return 0
    18         }
    19         
    20         var leftCount = 0
    21         var rightCount = 0
    22         
    23         var leftNode = root
    24         var rightNode = root
    25         while leftNode != nil{
    26             leftCount += 1
    27             leftNode = leftNode?.left
    28         }
    29         
    30         while rightNode != nil{
    31             rightCount += 1
    32             rightNode = rightNode?.right
    33         }
    34         if leftCount == rightCount{
    35             return Int( pow(Double(2), Double(leftCount)) - 1.0)
    36         }
    37 
    38         return countNodes(root?.left) + countNodes(root?.right) + 1
    39         
    40     }
    41 }

    100ms

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

    104ms

     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     var nodes = 0
    16     
    17     func countNodes(_ root: TreeNode?) -> Int {
    18         guard let root = root else { return 0 }
    19         nodes += 1
    20         if let left = root.left {
    21             countNodes(left)
    22         }
    23         if let right = root.right {
    24             countNodes(right)
    25         }
    26         return nodes
    27     }
    28 }

    112ms

     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 countNodes(_ root: TreeNode?) -> Int {
    16         if root == nil {
    17             return 0
    18         }
    19         var count = 0
    20         var stack = [TreeNode?]()
    21         stack.append(root)
    22         while !stack.isEmpty {
    23             guard let node = stack.removeLast() else {
    24                 return count
    25             } 
    26             count += 1
    27             if let lNode = node.left {
    28                 stack.append(lNode)
    29             }
    30             if let rNode = node.right {
    31                 stack.append(rNode)
    32             }
    33         }
    34         return count
    35     }
    36 }

    120ms

     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 countNodes(_ root: TreeNode?) -> Int {
    16     guard let root = root else {
    17         return 0
    18     }
    19     
    20     let left = countLeftNodes(root)
    21     let right = countRightNodes(root)
    22     
    23     if left == right {
    24         return NSDecimalNumber(decimal: Decimal(pow(2.0, Double(left)) - 1.0)).intValue
    25     }
    26     
    27     return countNodes(root.left) + countNodes(root.right) + 1
    28 }
    29 
    30 private func countLeftNodes(_ node: TreeNode?) -> Int {
    31     var height = 0, node = node
    32     
    33     while node != nil {
    34         node = node?.left
    35         height += 1
    36     }
    37     
    38     return height
    39 }
    40 
    41 private func countRightNodes(_ node: TreeNode?) -> Int {
    42     var height = 0, node = node
    43     while node != nil {
    44         node = node?.right
    45         height += 1
    46     }
    47     
    48     return height
    49 }
    50 }

    176ms

     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 countNodes(_ root: TreeNode?) -> Int {
    16         var hLeft:Int = leftHeight(root)
    17         var hRight:Int = rightHeight(root)
    18         if hLeft == hRight
    19         {
    20             return Int(pow(Double(2), Double(hLeft)) - 1)
    21         }
    22         return countNodes(root!.left) + countNodes(root!.right) + 1
    23     }
    24     
    25     func leftHeight(_ root: TreeNode?) -> Int
    26     {
    27         if root == nil {return 0}
    28         return 1 + leftHeight(root!.left)
    29     }
    30     
    31     func rightHeight(_ root: TreeNode?) -> Int
    32     {
    33         if root == nil {return 0}
    34         return 1 + rightHeight(root!.right)
    35     }
    36 }
  • 相关阅读:
    第四章:(2)原理之 Dubbo 框架设计
    大三学习进度29
    大三学习进度27
    大三学习进度31
    大三学习进度24
    大三学习进度29
    大三学习进度26
    大三学习进度28
    大三学习进度25
    大三学习进度32
  • 原文地址:https://www.cnblogs.com/strengthen/p/10203055.html
Copyright © 2020-2023  润新知