Opening your workspace…
Workspace/Coding room
Opening your workspace…
The cellar lift has one button for each depth of the storage tree. At closing time the lift mouse wants a tally of the trays to collect at each depth. Node i holds trays[i] trays. Node 0 is the root at depth 0 and has parent[0] = -1. Every other node i sits one level deeper than parent[i], whose index may be larger than i. Return the tray totals in depth order, from the root down to the deepest node. Keep a zero for an empty-handed stop so the buttons still line up.
[-1,0,0,1], [2,3,5,7]Output[2,8,7]The root holds 2 trays. Nodes 1 and 2 are at depth 1 and hold 3 + 5 = 8. Node 3 is at depth 2 and holds 7.
[-1], [0]Output[0]The root's depth is in the answer even though it holds no trays.
Go through the tree one level at a time, collecting the children of the current level to form the next one.
/** @param {number[]} parent * @param {number[]} trays * @returns {number[]} */ function solve(parent, trays) { // Return the tray total for each depth. }
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], [2,3,5,7][2,8,7]