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

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 }

原文地址:https://www.cnblogs.com/strengthen/p/10203055.html

时间: 2024-08-29 23:58:44

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

【LeetCode 222_完全二叉树_遍历】Count Complete Tree Nodes

解法一:递归 int countNodes(TreeNode* root) { if (root == NULL) return 0; TreeNode *pLeft = root->left; TreeNode *pRight = root->right; int ldepth = 0, rdepth = 0; while (pLeft) { ldepth++; pLeft = pLeft->left; } while (pRight) { rdepth++; pRight = pRi

[LeetCode][JavaScript]Count Complete Tree Nodes

Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. 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 le

【LeetCode】222. Count Complete Tree Nodes

Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. 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 le

8.8 LeetCode 222 Count Complete Tree Nodes

Question: Count Complete Tree Nodes Total Accepted: 11040 Total Submissions: 53992My Submissions Question Solution Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree

[LeetCode-JAVA] Count Complete Tree Nodes

题目: Given a complete binary tree, count the number of nodes. 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

LeetCode 222. 完全二叉树的节点个数(Count Complete Tree Nodes)

题目描述 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底层为第 h 层,则该层包含 1~ 2h 个节点. 示例: 输入: 1 / 2 3 / \ / 4 5 6 输出: 6 解题思路 从根节点开始分别判断左右子树的高度: 若左子树高度等于右子树,说明左子树一定为满二叉树,可得左子树的总节点个数,然后递归求右子树的节点数: 若左子树高度大于右子树

LeetCode OJ:Count Complete Tree Nodes(完全二叉树的节点数目)

Given a complete binary tree, count the number of nodes. 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 pos

leetcode 222 Count Complete Tree Nodes (计算完全二叉树节点数)

1. 问题描述 计算完全二叉树的节点数.对于完全二叉树的定义可参考wikipedia上面的内容. 2. 方法与思路 最简单也最容易想到的方法就是使用递归,分别递归计算左右子树的节点数的和.但此方法最容易超时,一般不可取. int countNodes(TreeNode* root) { if(root == NULL) return 0; else if(root->left == NULL) return 1; return countNodes(root->left) + countNod

[LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

Given a complete binary tree, count the number of nodes. 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 po