【Leetcode】124. Maximum path sum in binary tree (Hard)

1. Title

1. Topic description

A path in a binary tree is defined as a sequence of nodes with an edge between each pair of adjacent nodes in the sequence. The same node appears at most once in a path sequence . The path contains at least one node and does not necessarily pass through the root node.

The path sum is the sum of the values ​​of each node in the path.

Given the root node of a binary tree root, return its maximum path sum .

Example 1:
Insert image description here

Input: root = [1,2,3]
Output: 6
Explanation: The optimal path is 2 -> 1 -> 3, and the sum of the paths is 2 + 1 + 3 = 6

Example 2:
Insert image description here

Input: root = [-10,9,20,null,null,15,7]
Output: 42
Explanation: The optimal path is 15 -> 20 -> 7, and the sum of the paths is 15 + 20 + 7 = 42

Tips :

  • The range of the number of nodes in the tree is [1, 3 * 10

Guess you like

Origin blog.csdn.net/u011386173/article/details/132541668