Daily challenge for 2026-07-15
561. Count Tree Leaves from Parents
Given a 0-indexed array parent of size N representing a tree, where parent[i] is the parent of node i. The root of the tree has a parent of -1. Your task is to find the total number of leaf nodes in the tree. A leaf node is a node that does not have any children (i.e., its index never appears as a parent in the array).
Example:
Input: parent = [1, -1, 1, 2]
Output: 2
Explanation: Node 1 is the root. Node 0 and Node 2 have Node 1 as their parent. Node 3 has Node 2 as its parent. The nodes that have no children are Node 0 and Node 3. Thus, there are 2 leaf nodes.
Run this challenge interactively in Python or JavaScript when the app loads.