Quick Overview

A binary tree of depth at most 4 is given as sorted three-digit integers that encode each node's depth, position and value. Return the total of all root-to-leaf path sums. It tests decoding an implicit tree layout, finding parents and leaves from positions, and accumulating path sums in one pass.

Sum all root-to-leaf path sums of a binary tree encoded as three-digit integers

Company: Alibaba

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

If the depth of a tree is smaller than `5`, the tree can be represented by an array of three-digit integers. You are given an ascending array `nums` of three-digit integers that represents a binary tree with depth smaller than `5`. For each integer: - The hundreds digit is the depth `d` of the node, where `1 <= d <= 4`. - The tens digit is the position `p` of the node within its level, where `1 <= p <= 8`, counted as its position in a full binary tree. - The units digit is the value `v` of the node, where `0 <= v <= 9`. Return the sum of all root-to-leaf path sums: for every path from the root down to a leaf, add up the node values on that path, then add those path totals together. It is guaranteed that the array represents a valid connected binary tree. ### Function Signature ```python def path_sum(nums: list[int]) -> int: ``` ### Rules - The root is at depth 1, position 1. - Positions within a level are numbered from 1, left to right, as if the level were completely filled. A missing node still uses up its position. - A leaf is a node with no children in `nums`. - A node that lies on several root-to-leaf paths counts once for each of those paths. ### Constraints - `1 <= nums.length <= 15` - `110 <= nums[i] <= 489` - `nums` represents a valid binary tree with depth less than `5`. - `nums` is sorted in ascending order. ### Examples ```text Input: nums = [113, 215, 221] Output: 12 ``` The root has value 3, with a left child of value 5 and a right child of value 1. The two root-to-leaf paths sum to `3 + 5 = 8` and `3 + 1 = 4`, and `8 + 4 = 12`. ```text Input: nums = [113, 221] Output: 4 ``` The only depth-2 node is at position 2, so it is the root's right child. There is one path, `3 + 1 = 4`. ```text Input: nums = [113, 215, 221, 314, 342] Output: 18 ``` Node `314` (value 4) is the left child of `215`, and node `342` (value 2) sits at position 4 of depth 3, the right child of `221`. The paths are `3 + 5 + 4 = 12` and `3 + 1 + 2 = 6`, for a total of 18.

Overview: A binary tree of depth at most 4 is given as sorted three-digit integers that encode each node's depth, position and value. Return the total of all root-to-leaf path sums. It tests decoding an implicit tree layout, finding parents and leaves from positions, and accumulating path sums in one pass.

A binary tree whose depth is smaller than `5` can be written as an array of three-digit integers. You are given an ascending array `nums` of three-digit integers that represents a binary tree with depth smaller than `5`. For each integer: - The hundreds digit is the depth `d` of the node, where `1 <= d <= 4`. - The tens digit is the position `p` of the node within its level, where `1 <= p <= 8`, counted as its position in a full binary tree. - The units digit is the value `v` of the node, where `0 <= v <= 9`. Return the sum of all root-to-leaf path sums: for every path from the root down to a leaf, add up the node values on that path, then add those path totals together. It is guaranteed that the array represents a valid connected binary tree. ### Rules - The root is at depth 1, position 1. - Positions within a level are numbered from 1, left to right, as if the level were completely filled. A missing node still uses up its position. - A leaf is a node with no children in `nums`. - A node that lies on several root-to-leaf paths counts once for each of those paths. Implement `path_sum(nums)` and return the total as a single integer. ### Example 1 ```text Input: nums = [113, 215, 221] Output: 12 ``` The root has value 3, with a left child of value 5 and a right child of value 1. The two root-to-leaf paths sum to `3 + 5 = 8` and `3 + 1 = 4`, and `8 + 4 = 12`. ### Example 2 ```text Input: nums = [113, 215, 221, 314, 342] Output: 18 ``` Node `314` (value 4) is the left child of `215`, and node `342` (value 2) sits at position 4 of depth 3, the right child of `221`. The paths are `3 + 5 + 4 = 12` and `3 + 1 + 2 = 6`, for a total of 18. ### Constraints - `1 <= nums.length <= 15` - `110 <= nums[i] <= 489` - `nums` represents a valid binary tree with depth less than `5`. - `nums` is sorted in ascending order. The answer is at most 288 (8 paths of 4 nodes with value 9), so it always fits in a 32-bit signed integer.

Constraints

  • 1 <= nums.length <= 15
  • 110 <= nums[i] <= 489
  • nums represents a valid binary tree with depth less than 5.
  • nums is sorted in ascending order.
  • For each integer, the depth digit d, position digit p and value digit v satisfy 1 <= d <= 4, 1 <= p <= 8 and 0 <= v <= 9.
  • It is guaranteed that nums represents a valid connected binary tree.

Examples

Input: ([117],)

Expected Output: 7

Explanation: Minimum valid: a single root is its own leaf, so the answer is its value 7.

Input: ([110],)

Expected Output: 0

Explanation: Single root 110 with value 0: one path whose sum is 0.

Hints

  1. Start by splitting each three-digit integer into its depth, position and value, and keep them somewhere you can ask whether a node at a given depth and position exists.
  2. Positions are counted as if every level were completely filled. Work out which two positions on the next level sit directly below a node at position p.
  3. A node is a leaf only when neither of its possible children appears in nums, so a node with exactly one child is still on the way to a leaf.

Sum of Root-to-Leaf Paths in a Three-Digit Encoded Tree

mediumAlibabaSoftware Engineer

A binary tree whose depth is smaller than 5 can be written as an array of three-digit integers. You are given an ascending array nums of three-digit integers that represents a binary tree with depth smaller than 5. For each integer:

  • The hundreds digit is the depth d of the node, where 1 <= d <= 4.
  • The tens digit is the position p of the node within its level, where 1 <= p <= 8, counted as its position in a full binary tree.
  • The units digit is the value v of the node, where 0 <= v <= 9.

Return the sum of all root-to-leaf path sums: for every path from the root down to a leaf, add up the node values on that path, then add those path totals together.

It is guaranteed that the array represents a valid connected binary tree.

Rules

  • The root is at depth 1, position 1.
  • Positions within a level are numbered from 1, left to right, as if the level were completely filled. A missing node still uses up its position.
  • A leaf is a node with no children in nums.
  • A node that lies on several root-to-leaf paths counts once for each of those paths.

Implement path_sum(nums) and return the total as a single integer.

Example 1

Input: nums = [113, 215, 221]
Output: 12

The root has value 3, with a left child of value 5 and a right child of value 1. The two root-to-leaf paths sum to 3 + 5 = 8 and 3 + 1 = 4, and 8 + 4 = 12.

Example 2

Input: nums = [113, 215, 221, 314, 342]
Output: 18

Node 314 (value 4) is the left child of 215, and node 342 (value 2) sits at position 4 of depth 3, the right child of 221. The paths are 3 + 5 + 4 = 12 and 3 + 1 + 2 = 6, for a total of 18.

Constraints

  • 1 <= nums.length <= 15
  • 110 <= nums[i] <= 489
  • nums represents a valid binary tree with depth less than 5.
  • nums is sorted in ascending order.

The answer is at most 288 (8 paths of 4 nodes with value 9), so it always fits in a 32-bit signed integer.

Examples

Example 1
Input
([117],)
Output
7
Notes
Minimum valid: a single root is its own leaf, so the answer is its value 7.
Example 2
Input
([110],)
Output
0
Notes
Single root 110 with value 0: one path whose sum is 0.

Constraints

  • •1 <= nums.length <= 15
  • •110 <= nums[i] <= 489
  • •nums represents a valid binary tree with depth less than 5.
  • •nums is sorted in ascending order.
  • •For each integer, the depth digit d, position digit p and value digit v satisfy 1 <= d <= 4, 1 <= p <= 8 and 0 <= v <= 9.
  • •It is guaranteed that nums represents a valid connected binary tree.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def path_sum(nums: list[int]) -> int:
Sample call
path_sum([117])
Language
Case 1
Input
([117],)
Expected
7
Case 2
Input
([110],)
Expected
0

Browse More Questions

Show the approach

Approach

Decode every integer into its depth d = num // 100, position p = (num // 10) % 10 and value v = num % 10, and store v in a small table indexed by (d, p), where -1 means no node. Because positions are counted as if each level were full, the node at (d, p) has its left child at (d + 1, 2p - 1) and its right child at (d + 1, 2p). Walk the tree from the root (1, 1) with an explicit stack. Invariant: every stack entry is a node present in nums together with the sum of the values strictly above it on its unique path from the root. When a popped node has neither child in the table, it is a leaf, and its running total (which now includes its own value) is exactly that root-to-leaf path sum, so it is added to the answer. Otherwise each present child is pushed with the updated total. Each node is reached exactly once, through its single path from the root, so every root-to-leaf path is counted once, and a node shared by several paths contributes its value once to each of them. Edge cases: a lone root is its own leaf, so the answer is its value (0 for 110); a node with exactly one child is not a leaf; leaves can sit at any depth from 1 to 4; depth-4 nodes look up depth 5, which the table keeps empty. The result is at most 8 paths times 4 nodes times 9 = 288, so a 32-bit integer is enough in every language.

Time complexity:
O(n)
Space complexity:
O(n)