Count Complete Tree Nodes - Complete Solution Guide
Count Complete Tree Nodes is LeetCode problem 222, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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: roo
Detailed Explanation
The problem asks you to count the number of nodes in a complete binary tree. A complete binary tree is a binary tree in which all levels are completely filled except possibly the last level, and the last level has all keys as left as possible. The input is the root of the complete binary tree, and the output is the total number of nodes in the tree. The challenge is to design an algorithm that runs in less than O(n) time complexity, where n is the number of nodes.
Solution Approach
The provided solution employs a recursive divide-and-conquer strategy. It first checks if the left and right subtrees have the same height. If they do, it means the left subtree is a perfect binary tree. In this case, we can efficiently calculate the total nodes without traversing the entire subtree. If the heights are different, it means the right subtree is not a perfect binary tree, so it recursively counts nodes in both left and right subtrees and adds 1 (for the root node).
Step-by-Step Algorithm
- Step 1: Check if the root is null. If it is, return 0 (empty tree).
- Step 2: Calculate the height of the left subtree and the height of the right subtree by traversing down to the leftmost and rightmost leaves respectively.
- Step 3: If the left and right subtree heights are equal, the tree is essentially a perfect binary tree (or a perfect binary tree up to the second to last level). Calculate the total number of nodes using the formula (1 << (leftHeight + 1)) - 1 (or (1 << (rightHeight + 1)) - 1; both are equivalent in this case).
- Step 4: If the left and right subtree heights are unequal, the total number of nodes is 1 (the root) plus the number of nodes in the left subtree plus the number of nodes in the right subtree. Recursively call the `countNodes` function on the left and right subtrees to count their nodes.
- Step 5: Return the total count of nodes.
Key Insights
- Insight 1: A complete binary tree's left and right subtrees might have different heights. This difference is crucial in determining the algorithm's strategy.
- Insight 2: If the left and right subtrees have the same height, the tree is a perfect binary tree (or full up to the second to last level), allowing for a quick calculation of the total nodes using bit manipulation (2^(height+1) -1).
- Insight 3: A recursive approach, combined with the perfect binary tree check, optimizes the traversal by avoiding unnecessary recursive calls on the perfectly balanced portion of the tree.
Complexity Analysis
Time Complexity: O((log(n))^2)
Space Complexity: O(log(n))
Topics
This problem involves: Binary Search, Bit Manipulation, Tree, Binary Tree.
Companies
Asked at: Dunzo.