Problem 470337 · medium · Phase 04 Non-Linear Data Structures

Full and Half Nodes

binary trees · traversal · classification

Call a node full if it has two children and half if it has exactly one child. Given the root of a binary tree, return the list [full, half] with the number of full nodes and the number of half nodes.

Examples

      1
     / \
    2   3
   / \   \
  4   5   6

Input:  root = build_tree([1, 2, 3, 4, 5, None, 6])
Output: [2, 1]
Explanation: 1 and 2 are full; 3 has only a right child; 4, 5 and 6 are leaves.
Input:  root = build_tree([1, None, 2, None, 3])
Output: [0, 2]

Constraints

  • 0 <= number of nodes <= 2000
  • Target complexity: O(n) time.

Goals

  • Classify every node by how many children it has
  • Return two counts from a single traversal
  • Handle the empty tree and a single root
Starting Python…