employer cover photo
employer logo
employer logo

Lowe's Home Improvement

Engaged Employer

Lowe's Home Improvement Interview Question

"Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree."

Interview Answer

Anonymous

Aug 6, 2025

To find the maximum path sum in a binary tree, we use a recursive post-order traversal approach where we explore all possible paths from leaf to leaf or through any node. At each node, we calculate the maximum gain obtained by including either the left or right subtree (ignoring negative values), and then determine the maximum path sum that passes through the current node by adding its own value to both left and right gains. A global variable is maintained to track the highest path sum encountered during recursion. This method effectively handles negative node values and ensures the most optimal path is selected, even if the path does not pass through the root.