Arena / Problems /

222. Count Complete Tree Nodes

Medium 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #222

Given the root of a complete binary tree, return the number of the nodes in the tree.

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

Design an algorithm that runs in less than O(n) time complexity.

Example 1
Input: root = [1,2,3,4,5,6]
Output: 6
Example 2
Input: root = []
Output: 0
Example 3
Input: root = [1]
Output: 1
Constraints
  • The number of nodes in the tree is in the range [0, 5 * 10^4].
  • 0 <= Node.val <= 5 * 10^4
  • The tree is guaranteed to be complete.
⚔️

Vacant Python Throne

No developer has claimed the Python throne for LeetCode #222 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Python Solution by Anonymous Developer
0
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0

        # calc depth_left
        node = root
        depth_left = 1
        while node.left:
            depth_left += 1
            node = node.left

        # calc depth_right
        node = root
        depth_right = 1
        while node.right:
            depth_right += 1
            node = node.right

        if depth_left == depth_right: # It is a full binary tree.
            return 2 ** depth_left - 1 # key

        return self.countNodes(root.left) + self.countNodes(root.right) + 1
⚔️

Vacant Java Throne

No developer has claimed the Java throne for LeetCode #222 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant C++ Throne

No developer has claimed the C++ throne for LeetCode #222 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Javascript Throne

No developer has claimed the Javascript throne for LeetCode #222 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Go Throne

No developer has claimed the Go throne for LeetCode #222 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Rust Throne

No developer has claimed the Rust throne for LeetCode #222 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant C# Throne

No developer has claimed the C# throne for LeetCode #222 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Ruby Throne

No developer has claimed the Ruby throne for LeetCode #222 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Swift Throne

No developer has claimed the Swift throne for LeetCode #222 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Kotlin Throne

No developer has claimed the Kotlin throne for LeetCode #222 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.