An onboarding tour moves through a tree of dashboard widgets along parent-child links. Each widget has a score, which can be negative when a widget is confusing. A route is any path of one or more widgets connected by edges, never visiting a widget twice. It may start and end anywhere and doesn't have to pass through the root.
Write bestRoute(root) that returns the largest possible sum of scores along a route. The tree has at least one node.
Input: root = [2,4,5] Output: 11
4 → 2 → 5 sums to 11.
Input: root = [-30,9,20,null,null,15,-7] Output: 35
15 → 20 sums to 35. Going on through the root costs -30 and only gains 9.
1 <= number of widgets <= 3 * 10^4-1000 <= score <= 1000Trees are written level by level, like [1, 2, 3, null, 4], where null is a missing child. Your function works with TreeNode objects, each with val, left, and right.
Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #350 Best Scoring Route Through a Widget Tree. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.