Tree: Depth-First Search - Binary Tree Maximum Path SumUsing the same recursive code, what is the maximum path sum returned for a single-node tree with value -5?A-5B-10C0D5Check Answer
Step-by-Step SolutionSolution:Step 1: Trace max_gain for single node (-5)Left and right children are None, so left_gain=0, right_gain=0; price_newpath=-5+0+0=-5; max_sum=-5; return -5+max(0,0)=-5.Step 2: Return max_summax_sum is -5, which is the correct maximum path sum for this single-node tree.Final Answer:Option A -> Option AQuick Check:Negative node value is correctly handled [OK]Quick Trick: Max path sum can be negative if all nodes negative [OK]Common Mistakes:MISTAKESReturning 0 for negative single nodeIgnoring negative values in max_sumAssuming path sum must be non-negativeTrap Explanation:PITFALLCandidates often return 0 for empty or negative nodes, missing that max path sum can be negative.Interviewer Note:CONTEXTTests handling of edge cases with negative values and single nodes.
Master "Binary Tree Maximum Path Sum" in Tree: Depth-First Search3 interactive learning modes - each teaches the same concept differentlyTry ItSolutionTrace
More Tree: Depth-First Search Quizzes Binary Tree Cameras - Binary Tree Cameras - Quiz 11easy Binary Tree Inorder Traversal - Binary Tree Inorder Traversal - Quiz 6medium Binary Tree Inorder Traversal - Binary Tree Inorder Traversal - Quiz 14medium Binary Tree Postorder Traversal - Binary Tree Postorder Traversal - Quiz 2easy Construct Tree from Inorder and Postorder - Construct Tree from Inorder and Postorder - Quiz 9hard Construct Tree from Inorder and Postorder - Construct Tree from Inorder and Postorder - Quiz 4medium Construct Tree from Preorder and Inorder - Construct Tree from Preorder and Inorder - Quiz 6medium Diameter of Binary Tree - Diameter of Binary Tree - Quiz 12easy Lowest Common Ancestor of Binary Tree - Lowest Common Ancestor of Binary Tree - Quiz 9hard Maximum Depth of Binary Tree - Maximum Depth of Binary Tree - Quiz 5medium