- Difficulty: Medium
- Tags: LeetCode, Medium, Tree, Depth-First Search, Array, Hash Table, Binary Tree, leetcode-666, Breadth-First Search, O(n), O(w), 🔒, Topological Sort
Problem
If the depth of a tree is smaller than 5, then this tree can be represented by an array of three-digit integers. For each integer in this array:
- The hundreds digit represents the depth dof this node where1 <= d <= 4.
- The tens digit represents the position pof this node in the level it belongs to where1 <= p <= 8. The position is the same as that in a full binary tree.
- The units digit represents the value vof this node where0 <= v <= 9.
Given an array of ascending three-digit integers nums representing a binary tree with a depth smaller than 5, return the sum of all paths from the root towards the leaves.
It is guaranteed that the given array represents a valid connected binary tree.
Â
Example 1:
Input: nums = [113,215,221] Output: 12 Explanation: The tree that the list represents is shown. The path sum is (3 + 5) + (3 + 1) = 12.
Example 2:
Input: nums = [113,221] Output: 4 Explanation: The tree that the list represents is shown. The path sum is (3 + 1) = 4.
Â
Constraints:
- 1 <= nums.length <= 15
- 110 <= nums[i] <= 489
- numsrepresents a valid binary tree with depth less than- 5.