mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Update 0337.打家劫舍III.md
0337.打家劫舍|||新增C语言实现
This commit is contained in:
@ -490,6 +490,33 @@ function robNode(node: TreeNode | null): MaxValueArr {
|
||||
}
|
||||
```
|
||||
|
||||
### C
|
||||
|
||||
```c
|
||||
int *robTree(struct TreeNode *node) {
|
||||
int* amounts = (int*) malloc(sizeof(int) * 2);
|
||||
memset(amounts, 0, sizeof(int) * 2);
|
||||
if(node == NULL){
|
||||
return amounts;
|
||||
}
|
||||
int * left = robTree(node->left);
|
||||
int * right = robTree(node->right);
|
||||
// 偷当前节点
|
||||
amounts[1] = node->val + left[0] + right[0];
|
||||
// 不偷当前节点
|
||||
amounts[0] = max(left[0], left[1]) + max(right[0], right[1]);
|
||||
return amounts;
|
||||
}
|
||||
|
||||
int rob(struct TreeNode* root) {
|
||||
int * dp = robTree(root);
|
||||
// 0代表不偷当前节点可以获得的最大值,1表示偷当前节点可以获取的最大值
|
||||
return max(dp[0], dp[1]);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Rust
|
||||
|
||||
动态规划:
|
||||
@ -523,4 +550,3 @@ impl Solution {
|
||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||
</a>
|
||||
|
||||
|
Reference in New Issue
Block a user