Codepath

Count the Tree Leaves

Unit 8 Session 1 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10 mins
  • 🛠️ Topics: Binary Tree, Tree Traversal, Recursion

1: U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • What is a leaf node in a binary tree?
    • A leaf node is a node that does not have any children.
  • How should the function behave if the tree is empty?
    • The function should return 0 if the tree is empty.
HAPPY CASE
Input: A binary tree with three leaf nodes
Output: 3
Explanation: The function correctly counts the number of leaf nodes.

EDGE CASE
Input: A binary tree with no leaf nodes (only root)
Output: 1
Explanation: The root itself is counted as a leaf node since it has no children.

2: M-atch

Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.

For Tree Counting problems, we want to consider the following approaches:

  • Binary Tree Traversal: Traverse the tree to identify and count the leaf nodes.
  • Recursion: Use recursion to count the leaves in the left and right subtrees.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the tree recursively, checking if each node is a leaf, and count the number of leaf nodes.

1) If the current node is None, return 0.
2) If the current node is a leaf node (no left or right children), return 1.
3) Recursively count the number of leaves in the left subtree.
4) Recursively count the number of leaves in the right subtree.
5) Return the sum of the leaf counts from the left and right subtrees.

⚠️ Common Mistakes

  • Not accounting for cases where the tree is empty (root is None).
  • Incorrectly counting non-leaf nodes as leaves.

4: I-mplement

Implement the code to solve the algorithm.

class TreeNode:
    def __init__(self, value, left=None, right=None):
        self.val = value
        self.left = left
        self.right = right

def count_leaves(root):
    if root is None:
        return 0
    
    # If the node is a leaf, return 1
    if root.left is None and root.right is None:
        return 1
    
    # Recursively count the leaves in the left and right subtrees
    left_leaves = count_leaves(root.left)
    right_leaves = count_leaves(root.right)
    
    return left_leaves + right_leaves

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Test with the examples given:
    • Input 1: Binary tree with three leaf nodes
    • Expected Output: 3
    • Input 2: Binary tree with one leaf node
    • Expected Output: 1
    • Verify that the outputs are correct.

6: E-valuate

Evaluate the performance of your algorithm and state any strong/weak or future potential work.

Assume N represents the number of nodes in the binary tree.

  • Time Complexity: O(N) because the algorithm needs to traverse all the nodes in the tree to count the leaves.
  • Space Complexity: O(H) where H is the height of the tree, due to the recursive call stack.
Fork me on GitHub