目录

lc124.二叉树中的最大路径和

124.二叉树中的最大路径和

1
2
    a
b       c
  • 最大路径可能有三种情况
    • $b \rightarrow a \rightarrow c $
    • $b \rightarrow a \rightarrow a’father$
    • $c \rightarrow a \rightarrow a’father$
  • 该节点向上返回值为该节点能够提供的最大路径
    • 小于零时,舍弃该结点的儿子结点。
    • 如果以该节点作为最优路径根节点
      • $path = left + right + root -> val$
    • 如果该节点作为左右子树的结点
      • $return \ max(left, right) + root -> val$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int ans = INT_MIN;
    int maxPathSum(TreeNode* root) {
        dfs(root);
        return ans;
    }

    // return root substree maxval
    int dfs(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }

        int l = dfs(root->left);
        int r = dfs(root->right);

        // subtree value
        ans = max(l + r + root->val, ans);

        return max(0, max(l, r) + root->val);
    }
};

类似题目