Home › Company-wise questions › Deloitte › Binary Tree Maximum Path Sum ← Deloitte bank
Deloitte · Question 69 · Trees
Binary Tree Maximum Path Sum Trees Java code ⏱ O(n) 💾 O(h)
Service-based Published and maintained by DSA-topics · Last updated 30 November 2026 · Report an error
About this bank These are classic technical-interview concepts compiled from publicly circulated company-wise question sets, rewritten with original explanations and compiler-ready solutions for educational practice; inclusion does not imply endorsement by the company. Spotted an error? Tell us .
Problem Path can start and end anywhere; compute for each node the max path through it and update the global max using postorder.
Approach DFS returns the best downward gain, dropping negatives.
Time O(n)Space O(h)
How it works 1 Each DFS returns the best downward gain, dropping negatives. 2 Update the global best with the through-node sum. Sample input / output Input:
root = [-10, 9, 20, null, null, 15, 7]
Output:
42 Constraints 1 <= nodes <= 3 * 10^4.
Solution — read it, then walk it Flowchart Pseudocode
Time O(n)Space O(h)Loops 2Branches 7Returns 4source · Java
Scroll sideways for wide charts — the view follows the active step. ⤢ Expand
↻ repeat ↻ repeat START import java.util.* DEFINE class Main DEFINE class TreeNode DEFINE val TreeNode left, right TreeNode(int v) { val = v; } DEFINE TreeNode buildTree(Integer[] values) if (values.length == 0 || values[0] == null) return null TreeNode root = new TreeNode(values[0]) java.util.Queue<TreeNode> q = new java.util.LinkedList<>() q.offer(root) DEFINE i = 1 while (!q.isEmpty() && i < values.length) TreeNode node = q.poll() if (i < values.length && values[i] != null) { node.left = ne… i++ if (i < values.length && values[i] != null) { node.right = ne… i++ RETURN root DEFINE String levelOrder(TreeNode root) if (root == null) return "[]" StringBuilder sb = new StringBuilder("[") java.util.Queue<TreeNode> q = new java.util.LinkedList<>() q.offer(root) DEFINE first = true while (!q.isEmpty()) TreeNode node = q.poll() if (node == null) continue if (!first) sb.append(", ") first = false sb.append(node.val) q.offer(node.left) q.offer(node.right) RETURN sb.append("]").toString() DEFINE int max DEFINE static int maxPathSum(TreeNode root) max = Integer.MIN_VALUE dfs(root) RETURN max DEFINE static int dfs(TreeNode r) if (r == null) return 0 DEFINE left = Math.max(0, dfs(r.left)) DEFINE right = Math.max(0, dfs(r.right)) max = Math.max(max, left + right + r.val) RETURN Math.max(left, right) + r.val start / end statement condition loop returns definition← Q68 · Lowest Common Ancestor of Binary Tree Q70 · Word Ladder (Length) → Solution code Core logic highlighted in green
Language Python Javasource C++ C Core logic Full code Copy
Recommended resources Affiliate links · may earn us a commission
Classic DSA interview books The reference books interviewers themselves learned from — great next to the visualizers. See the books → Structured coding courses Video courses that pair well with visual practice — pick one language and go deep. Browse courses → Cloud hosting for side projects Ship the portfolio project that gets you hired — free credits for new accounts. Claim credits →