Count Complete Tree Nodes (Optimized)

Data Structures
Medium
Microsoft
34K views

Given the root of a complete binary tree, return the number of nodes in the tree. Solve in $O((\log N)^2)$ time by utilizing the structure of a complete tree.

Why Interviewers Ask This

Microsoft interviewers ask this to evaluate if candidates can leverage specific data structure properties rather than applying brute-force solutions. They assess your ability to analyze tree depth, recognize the definition of a complete binary tree, and optimize time complexity from O(N) to O(log² N). This tests algorithmic efficiency and deep understanding of binary heap structures.

How to Answer This Question

1. Clarify the Definition: Immediately define a complete binary tree where every level is fully filled except possibly the last, which is filled left-to-right. State that a standard traversal yields O(N), which fails the constraint. 2. Analyze Depth: Explain calculating the leftmost path height (h_left) and rightmost path height (h_right) in O(log N). 3. Identify Full Subtrees: If h_left equals h_right, the tree is a perfect binary tree. Use the formula 2^h - 1 to return the count instantly in O(1). 4. Recursive Optimization: If heights differ, the root has a full left subtree but an incomplete right one. Recursively count nodes in both subtrees, adding one for the root. 5. Complexity Check: Ensure you articulate why the recursion depth is logarithmic and each step takes logarithmic time, resulting in O(log² N).

Key Points to Cover

  • Explicitly defining the structural difference between complete and perfect binary trees
  • Demonstrating how to calculate tree height by traversing only the leftmost and rightmost paths
  • Applying the mathematical formula 2^h - 1 for perfect subtrees to avoid unnecessary traversal
  • Deriving the O(log² N) time complexity through recurrence analysis rather than guessing
  • Showing awareness of Microsoft's focus on optimizing algorithms beyond basic correctness

Sample Answer

To solve this efficiently for Microsoft's standards, I first need to distinguish between a general binary tree and a complete one. A naive DFS or BFS would visit every node, taking O(N) time, which violates the requirement. Since we are given a complete binary tree, I can exploit its structural property: levels are filled left-to-right. My strategy involves comparing the height of the leftmost path versus the rightmost path. I will traverse down the left children to find the maximum depth, and similarly for the right children. Both operations take O(log N) time since the height of a balanced tree is logarithmic. If these two heights are equal, the tree is a perfect binary tree. In this case, I don't need to visit any nodes; I can directly calculate the total count using the formula 2^height minus 1. This gives me an O(1) solution for this branch. If the heights differ, it means the last level is not full. However, because it is complete, the left subtree must be a perfect binary tree of height h-1. The right subtree might be incomplete. I recursively apply the same logic to the right child while adding the size of the known perfect left subtree (2^(h-1) - 1) plus the root node. By repeating this, the recurrence relation becomes T(n) = T(n/2) + O(log n), which solves to O(log² n). This approach minimizes node visits significantly compared to standard traversal.

Common Mistakes to Avoid

  • Performing a full DFS or BFS traversal which results in O(N) time complexity and misses the optimization opportunity
  • Confusing the definition of a complete tree with a full or perfect tree, leading to incorrect height comparisons
  • Failing to explain the mathematical derivation for the perfect subtree count, making the solution seem like magic
  • Ignoring the edge case where the tree is empty or has only one node without handling it gracefully

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 65 Microsoft questions