As reported: Amazon SDE-1 university-hire interview (March–April 2025), round 1 — after an object-oriented question: count the nodes of a binary tree that have both children, with a post-order recursive solution and an iterative follow-up.
Given the root of a binary tree, return the number of nodes that have both a left child and a right child.
Example 1
root = [1,2,3,4,5,null,6]
Output: 2 (nodes 1 and 2)
Example 2
root = [1,2,null,3]
Output: 0
Constraints: 0 ≤ number of nodes ≤ 10⁵; −10⁹ ≤ Node.val ≤ 10⁹.
Code runner format: one line: the tree as a level-order JSON array with null for missing children (LeetCode's format). Implement countFullNodes(root); the harness prints the count.
Follow-ups
- Solve it without recursion.