solution.md 3.8 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 二叉树中的最大路径和
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
3 4 5 6 7 8
<p><strong>路径</strong> 被定义为一条从树中任意节点出发,沿父节点-子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 <strong>至多出现一次</strong> 。该路径<strong> 至少包含一个 </strong>节点,且不一定经过根节点。</p>

<p><strong>路径和</strong> 是路径中各节点值的总和。</p>

<p>给你一个二叉树的根节点 <code>root</code> ,返回其 <strong>最大路径和</strong></p>

F
fix bug  
feilong 已提交
9

每日一练社区's avatar
每日一练社区 已提交
10 11

<p><strong>示例 1:</strong></p>
每日一练社区's avatar
每日一练社区 已提交
12
<img alt="" src="https://img-blog.csdnimg.cn/img_convert/ca0d0d9f728f2aeda1976de9b08940e5.png#pic_center" />
每日一练社区's avatar
每日一练社区 已提交
13 14
<pre>
<strong>输入:</strong>root = [1,2,3]
每日一练社区's avatar
每日一练社区 已提交
15

每日一练社区's avatar
每日一练社区 已提交
16
<strong>输出:</strong>6
每日一练社区's avatar
每日一练社区 已提交
17

每日一练社区's avatar
每日一练社区 已提交
18 19 20
<strong>解释:</strong>最优路径是 2 -> 1 -> 3 ,路径和为 2 + 1 + 3 = 6</pre>

<p><strong>示例 2:</strong></p>
每日一练社区's avatar
每日一练社区 已提交
21
<img alt="" src="https://img-blog.csdnimg.cn/img_convert/730fd58fb57c0fbc6d5f77b18e9d6b50.png#pic_center" />
每日一练社区's avatar
每日一练社区 已提交
22 23
<pre>
<strong>输入:</strong>root = [-10,9,20,null,null,15,7]
每日一练社区's avatar
每日一练社区 已提交
24

每日一练社区's avatar
每日一练社区 已提交
25
<strong>输出:</strong>42
每日一练社区's avatar
每日一练社区 已提交
26

每日一练社区's avatar
每日一练社区 已提交
27 28 29
<strong>解释:</strong>最优路径是 15 -> 20 -> 7 ,路径和为 15 + 20 + 7 = 42
</pre>

F
fix bug  
feilong 已提交
30

每日一练社区's avatar
每日一练社区 已提交
31 32 33 34 35 36 37 38

<p><strong>提示:</strong></p>

<ul>
	<li>树中节点数目范围是 <code>[1, 3 * 10<sup>4</sup>]</code></li>
	<li><code>-1000 <= Node.val <= 1000</code></li>
</ul>

每日一练社区's avatar
每日一练社区 已提交
39
<p>以下<span style="color:red">错误</span>的选项是?</p>
F
fix bug  
feilong 已提交
40

每日一练社区's avatar
每日一练社区 已提交
41
## aop
F
fix bug  
feilong 已提交
42

每日一练社区's avatar
每日一练社区 已提交
43
### before
F
fix bug  
feilong 已提交
44

每日一练社区's avatar
每日一练社区 已提交
45
```c
每日一练社区's avatar
每日一练社区 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
#include <bits/stdc++.h>
using namespace std;

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) {}
};
```
每日一练社区's avatar
每日一练社区 已提交
59

每日一练社区's avatar
每日一练社区 已提交
60
### after
F
fix bug  
feilong 已提交
61

每日一练社区's avatar
每日一练社区 已提交
62
```c
每日一练社区's avatar
每日一练社区 已提交
63 64 65 66

```

## 答案
F
fix bug  
feilong 已提交
67

每日一练社区's avatar
每日一练社区 已提交
68
```c
每日一练社区's avatar
每日一练社区 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
class Solution
{
public:
    int maxv = -123123632;
    int maxPathSum(TreeNode *root)
    {
        if (root == NULL)
            return 0;
        calc(root);
        return maxv;
    }

    int calc(TreeNode *root)
    {
        if (root == NULL)
            return 0;
        int temp = root->val;
        int lmaxsum = calc(root->left);
        int rmaxsum = calc(root->right);

        if (lmaxsum > 0)
            temp += lmaxsum;
        if (rmaxsum > 0)
            temp += rmaxsum;
        return max(root->val, max(root->val + lmaxsum, root->val + rmaxsum));
    }
};
```
## 选项

F
fix bug  
feilong 已提交
99

每日一练社区's avatar
每日一练社区 已提交
100
### A
F
fix bug  
feilong 已提交
101

每日一练社区's avatar
每日一练社区 已提交
102
```c
每日一练社区's avatar
每日一练社区 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
class Solution
{
    int res = INT_MIN;

public:
    int maxPathSum(TreeNode *root)
    {

        int p = getdfs(root);
        return res;
    }
    int getdfs(TreeNode *node)
    {
        if (node == NULL)
            return 0;
        int left = max(getdfs(node->left), 0);
        int right = max(getdfs(node->right), 0);
        int size = left + right + node->val;
        res = res > size ? res : size;
        right = right > left ? right : left;
        node->val = right + node->val;
        return node->val;
    }
};
```

### B
F
fix bug  
feilong 已提交
130

每日一练社区's avatar
每日一练社区 已提交
131
```c
每日一练社区's avatar
每日一练社区 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
class Solution
{
public:
    int ans = 0;
    int OneSideMax(TreeNode *root)
    {
        if (root == nullptr)
            return 0;

        int left = max(0, OneSideMax(root->left));
        int right = max(0, OneSideMax(root->right));

        ans = max(ans, left + right + root->val);
        return max(left, right) + root->val;
    }

    int maxPathSum(TreeNode *root)
    {
        OneSideMax(root);
        return ans;
    }
};
```

### C
F
fix bug  
feilong 已提交
157

每日一练社区's avatar
每日一练社区 已提交
158
```c
每日一练社区's avatar
每日一练社区 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
class Solution
{
public:
    int dfs(TreeNode *root, int &res)
    {
        if (root == NULL)
        {
            return 0;
        }
        int left = max(dfs(root->left, res), 0);
        int right = max(dfs(root->right, res), 0);
        res = max(res, root->val + left + right);

        return root->val + max(left, right);
    }
    int maxPathSum(TreeNode *root)
    {
        int res = INT_MIN;
        dfs(root, res);
        return res;
    }
};
```