Opening your workspace…
Workspace/Coding room
Opening your workspace…
The brewery stores ingredients on a branching shelf system. A leaf is a shelf with no children. Return the sum of stock[i] only at leaf shelves; stock on internal shelves does not count. A single shelf is itself a leaf. The tree has nodes numbered 0 through n - 1. parent[0] is -1; every other parent[i] is the parent of node i. The tree is connected and has no cycles. Node numbers do not imply traversal order: a parent may have a larger number than its child.
[-1,0,0,1], [50,20,4,7]Output11Shelves 2 and 3 have no children, so only their 4 + 7 units count.
[-1], [9]Output9The only shelf has no children and is therefore a leaf.
A shelf is internal precisely when its index appears as the parent of another shelf.
Extend the same parent representation into a level-by-level traversal.
Try this problem →Class A · Trees / Depth-first searchCount complete descendant branches instead of only their end shelves.
Try this problem →/** @returns {number} */ function solve(parent, stock) { // Return the result described above. }
Tab inserts two spaces. Shift+Tab outdents. Use the arrow keys and Tab to choose a suggestion. Press Escape, then Tab, to leave the editor. Press Control or Command with apostrophe to run samples or with Enter to submit.
[-1,0,0,1], [50,20,4,7]11