Imagine you're working with a compact binary tree representation where each node is encoded as a special three-digit number! ๐ณ
You're given an array of these three-digit integers representing a binary tree with depth less than 5. Each number follows this encoding:
- Hundreds digit (d): The depth/level of the node (1 โค d โค 4)
- Tens digit (p): The position within that level (1 โค p โค 8)
- Units digit (v): The actual value of the node (0 โค v โค 9)
Your mission: Calculate the sum of all root-to-leaf paths in this encoded tree!
For example, if a path from root to leaf has values [5, 4, 2], that path contributes 5 + 4 + 2 = 11 to the total sum.
Note: The array represents a valid connected binary tree, so you don't need to worry about invalid structures.
Input & Output
Time & Space Complexity
Each of the n nodes is visited exactly once during the DFS traversal, with O(1) hash lookups
Hash map stores n coordinate-value pairs, plus O(h) recursion stack where h โค 4
Constraints
- 1 โค nums.length โค 15
- 1 โค nums[i] โค 103
- The depth of the tree is at most 4
- Each number represents a valid node in format ddd where d is depth (1-4), p is position (1-8), v is value (0-9)
- The given array represents a valid connected binary tree