Binary Tree Maximum Path Sum
Updated
Problem statement
Find a nonempty path in a binary tree with the largest sum of node values. Return that sum. A path uses parent-child edges, visits each node at most once, and may begin and end anywhere; it need not include the root.
For this exercise, use the conventional maximum-path-sum interpretation of the named tree exercise. The arrays values, left and right describe the nodes. Node 0 is the root. A child index of -1 means there is no child; any other child index identifies that child node. Inputs describe one valid connected binary tree. Return the sum as a signed 64-bit integer.
Function
maximumPathSum(values: int[], left: int[], right: int[]) → longExamples
Example 1
values = [-10,9,20,15,7]left = [1,-1,3,-1,-1]right = [2,-1,4,-1,-1]return = 42The best path is node 3 to node 2 to node 4, totaling 15+20+7.
Example 2
values = [-3]left = [-1]right = [-1]return = -3A nonempty path is required even when every value is negative.
Example 3
values = [1,2,3]left = [1,-1,-1]right = [2,-1,-1]return = 6The path through both children and the root has sum 6.
Constraints
- The three arrays have the same length
1 <= n <= 200. -10^6 <= values[i] <= 10^6.- Child indices are -1 or valid node indices. Node 0 is the root; every other node has exactly one parent.
- The input is connected and acyclic. The path contains at least one node.
More ByteDance Onsite Interview questions
- Search a Row- and Column-Sorted MatrixONSITE INTERVIEW · Seen Nov 2026
- Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Implement Multi-Head AttentionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dynamic Median with Additions and DeletionsONSITE INTERVIEW · Seen Oct 2026
- Serialize and Deserialize a String Key-Value StoreONSITE INTERVIEW · Seen Oct 2026
- Merge Overlapping IntervalsONSITE INTERVIEW · Seen Oct 2026
- Tic-Tac-Toe Move WinnersONSITE INTERVIEW · Seen Oct 2026
- Kth Largest Element without a HeapONSITE INTERVIEW · Seen Oct 2026
- Least Recently Used CachePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Boolean Expression Results After Leaf FlipsGoogle · ONSITE INTERVIEWGoogle interview questions
- Transitive Employee Referral CountsRobinhood · ONSITE INTERVIEWRobinhood interview questions
- Maximum Nonadjacent Sum in a General TreeZip · PHONE SCREEN · ONSITE INTERVIEWZip interview questions
- Maximum Sum of Non-Adjacent Tree NodesInMobi · ONSITE INTERVIEWInMobi interview questions
- Distance Sums to Special Nodes in a Weighted TreeNutanix · ONSITE INTERVIEWNutanix interview questions
- File Encryption Tree OptimizationDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
- Binary Tree Maximum Path SumGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Binary Tree Maximum Path SumBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Binary Tree Maximum Path SumGlobalization Partners · ONSITE INTERVIEW
- Binary Tree Maximum Path SumJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
ByteDance interview experiences
- ByteDance TikTok USDS SWE Intern Interview: 1 Experience
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience