funcmax(nums...int)int{res:=0for_,val:=rangenums{ifval>res{res=val}}returnres}/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcrob(root*TreeNode)int{varpostorderfunc(root*TreeNode)(int,int)postorder=func(root*TreeNode)(int,int){ifnil==root{// 第一个值是偷取 root 获取的最大金额
// 第二个值是不偷取 root 获取的最大金额
return0,0}left,leftNo:=postorder(root.Left)right,rightNo:=postorder(root.Right)first:=leftNo+rightNo+root.Valsecond:=max(left,leftNo)+max(right,rightNo)returnfirst,second}a,b:=postorder(root)returnmax(a,b)}