Skip to content

Commit

Permalink
337.打家劫舍Ⅲ 增加go的解法 树形DP
Browse files Browse the repository at this point in the history
  • Loading branch information
baici1 committed Oct 15, 2021
1 parent f88678d commit 99d28fc
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions problems/0337.打家劫舍III.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,48 @@ class Solution:
return (val1, val2)
```

Go:

树形DP

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func rob(root *TreeNode) int {
return max(robTree(root))
}
func robTree(root *TreeNode)(int,int){
if root==nil{
return 0,0
}
//获取左节点的偷的值与不偷的值
left0,left1:=robTree(root.Left)
//获取右节点的偷的值与不偷的值
right0,right1:=robTree(root.Right)
//
val1:=root.Val
val1+=left1+right1
//不偷
val2:=0
val2+=max(left0,left1)+max(right0,right1)
return val1,val2
}
func max(a,b int)int{
if a>b{
return a
}
return b
}
```



JavaScript:

> 动态规划
Expand Down

0 comments on commit 99d28fc

Please sign in to comment.