java.lang.Object
g0101_0200.s0124_binary_tree_maximum_path_sum.Solution

public class Solution extends Object
124 - Binary Tree Maximum Path Sum.<p>Hard</p> <p>A <strong>path</strong> in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence <strong>at most once</strong>. Note that the path does not need to pass through the root.</p> <p>The <strong>path sum</strong> of a path is the sum of the node&rsquo;s values in the path.</p> <p>Given the <code>root</code> of a binary tree, return <em>the maximum <strong>path sum</strong> of any <strong>non-empty</strong> path</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg" alt="" /></p> <p><strong>Input:</strong> root = [1,2,3]</p> <p><strong>Output:</strong> 6</p> <p><strong>Explanation:</strong> The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg" alt="" /></p> <p><strong>Input:</strong> root = [-10,9,20,null,null,15,7]</p> <p><strong>Output:</strong> 42</p> <p><strong>Explanation:</strong> The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 3 * 10<sup>4</sup>]</code>.</li> <li><code>-1000 <= Node.val <= 1000</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxPathSum

      public int maxPathSum(TreeNode root)