|
49 | 49 |
|
50 | 50 | <!-- 这里可写通用的实现逻辑 -->
|
51 | 51 |
|
| 52 | +思考二叉树递归问题的经典套路: |
| 53 | + |
| 54 | +1. 终止条件(何时终止递归) |
| 55 | +2. 递归处理左右子树 |
| 56 | +3. 合并左右子树的计算结果 |
| 57 | + |
| 58 | +对于本题,由于要满足题目对“路径”的定义,在返回当前子树对外贡献的最大路径和时,需要取 `left`,`right` 的最大值 |
| 59 | + |
52 | 60 | <!-- tabs:start -->
|
53 | 61 |
|
54 | 62 | ### **Python3**
|
55 | 63 |
|
56 | 64 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
57 | 65 |
|
58 | 66 | ```python
|
59 |
| - |
| 67 | +# Definition for a binary tree node. |
| 68 | +# class TreeNode: |
| 69 | +# def __init__(self, val=0, left=None, right=None): |
| 70 | +# self.val = val |
| 71 | +# self.left = left |
| 72 | +# self.right = right |
| 73 | +class Solution: |
| 74 | + def maxPathSum(self, root: TreeNode) -> int: |
| 75 | + ans = float("-inf") |
| 76 | + |
| 77 | + def dfs(node: TreeNode) -> int: |
| 78 | + if not node: |
| 79 | + return 0 |
| 80 | + left = max(0, dfs(node.left)) |
| 81 | + right = max(0, dfs(node.right)) |
| 82 | + nonlocal ans |
| 83 | + ans = max(ans, node.val + left + right) |
| 84 | + return node.val + max(left, right) |
| 85 | + |
| 86 | + dfs(root) |
| 87 | + return ans |
60 | 88 | ```
|
61 | 89 |
|
62 | 90 | ### **Java**
|
63 | 91 |
|
64 | 92 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
65 | 93 |
|
66 | 94 | ```java
|
| 95 | +/** |
| 96 | + * Definition for a binary tree node. |
| 97 | + * public class TreeNode { |
| 98 | + * int val; |
| 99 | + * TreeNode left; |
| 100 | + * TreeNode right; |
| 101 | + * TreeNode() {} |
| 102 | + * TreeNode(int val) { this.val = val; } |
| 103 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 104 | + * this.val = val; |
| 105 | + * this.left = left; |
| 106 | + * this.right = right; |
| 107 | + * } |
| 108 | + * } |
| 109 | + */ |
| 110 | +class Solution { |
| 111 | + private int ans = Integer.MIN_VALUE; |
| 112 | + |
| 113 | + public int maxPathSum(TreeNode root) { |
| 114 | + dfs(root); |
| 115 | + return ans; |
| 116 | + } |
| 117 | + |
| 118 | + private int dfs(TreeNode node) { |
| 119 | + if (node == null) { |
| 120 | + return 0; |
| 121 | + } |
| 122 | + int left = Math.max(0, dfs(node.left)); |
| 123 | + int right = Math.max(0, dfs(node.right)); |
| 124 | + ans = Math.max(ans, node.val + left + right); |
| 125 | + return node.val + Math.max(left, right); |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +### **Go** |
| 131 | + |
| 132 | +```go |
| 133 | +/** |
| 134 | + * Definition for a binary tree node. |
| 135 | + * type TreeNode struct { |
| 136 | + * Val int |
| 137 | + * Left *TreeNode |
| 138 | + * Right *TreeNode |
| 139 | + * } |
| 140 | + */ |
| 141 | +func maxPathSum(root *TreeNode) int { |
| 142 | + ans := math.MinInt32 |
| 143 | + |
| 144 | + var dfs func(*TreeNode) int |
| 145 | + dfs = func(node *TreeNode) int { |
| 146 | + if node == nil { |
| 147 | + return 0 |
| 148 | + } |
| 149 | + left := max(0, dfs(node.Left)) |
| 150 | + right := max(0, dfs(node.Right)) |
| 151 | + ans = max(ans, node.Val+left+right) |
| 152 | + return node.Val + max(left, right) |
| 153 | + } |
| 154 | + |
| 155 | + dfs(root) |
| 156 | + return ans |
| 157 | +} |
| 158 | + |
| 159 | +func max(a, b int) int { |
| 160 | + if a > b { |
| 161 | + return a |
| 162 | + } |
| 163 | + return b |
| 164 | +} |
| 165 | +``` |
67 | 166 |
|
| 167 | +### **C++** |
| 168 | + |
| 169 | +```cpp |
| 170 | +/** |
| 171 | + * Definition for a binary tree node. |
| 172 | + * struct TreeNode { |
| 173 | + * int val; |
| 174 | + * TreeNode *left; |
| 175 | + * TreeNode *right; |
| 176 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 177 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 178 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 179 | + * }; |
| 180 | + */ |
| 181 | +class Solution { |
| 182 | +public: |
| 183 | + int maxPathSum(TreeNode* root) { |
| 184 | + int ans = INT_MIN; |
| 185 | + |
| 186 | + function<int(TreeNode*)> dfs = [&](TreeNode* node) { |
| 187 | + if (node == nullptr) { |
| 188 | + return 0; |
| 189 | + } |
| 190 | + int left = max(0, dfs(node->left)); |
| 191 | + int right = max(0, dfs(node->right)); |
| 192 | + ans = max(ans, node->val + left + right); |
| 193 | + return node->val + max(left, right); |
| 194 | + }; |
| 195 | + |
| 196 | + dfs(root); |
| 197 | + return ans; |
| 198 | + } |
| 199 | +}; |
68 | 200 | ```
|
69 | 201 |
|
70 | 202 | ### **...**
|
|
0 commit comments