diff --git a/problems/0337.打家劫舍III.md b/problems/0337.打家劫舍III.md index 949137c3..204c3815 100644 --- a/problems/0337.打家劫舍III.md +++ b/problems/0337.打家劫舍III.md @@ -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: > 动态规划