动态规划_leetcode337

#coding=utf-8

# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None


# 递归
class Solution1(object):
def rob(self, root):
"""
:type root: TreeNode
:rtype: int
"""

if not root :
return 0

return self.tryRob(root)


def tryRob(self,root):


if not root:
return 0

if root and not root.left and not root.right:
return root.val


leftChild = root.left
rightChild = root.right

res = root.val


if leftChild:
res += self.tryRob(leftChild.left)
res += self.tryRob(leftChild.right)
if rightChild:
res += self.tryRob(rightChild.left)
res += self.tryRob(rightChild.right)


res = max(res,self.tryRob(leftChild)+self.tryRob(rightChild))

return res


def createTree1(self):

root = TreeNode(3)
root.left = TreeNode(4)
root.right = TreeNode(5)

root.left.left = TreeNode(1)
root.left.right = TreeNode(3)

root.right.right = TreeNode(1)



return root


def createTree2(self):
root = TreeNode(3)

root.left = TreeNode(2)
root.right = TreeNode(3)

root.left.right = TreeNode(3)
root.right.right = TreeNode(1)

return root



# s = Solution1()
#
# root = s.createTree2()
#
# print s.rob(root)


# 记忆化递归
class Solution2(object):
def rob(self, root):
"""
:type root: TreeNode
:rtype: int
"""

if not root :
return 0

self.memo = {}


return self.tryRob(root)


def tryRob(self,root):


if self.memo.has_key(root):
return self.memo[root]

if not root:
return 0

if root and not root.left and not root.right:
self.memo[root] = root.val
return root.val

else:

res1 = self.tryRob(root.left) + self.tryRob(root.right)

res2 = root.val

if root.left:
res2 += self.tryRob(root.left.left)
res2 += self.tryRob(root.left.right)

if root.right:
res2 += self.tryRob(root.right.left)
res2 += self.tryRob(root.right.right)


res = max(res1,res2)
self.memo[root] = res

return res




def createTree1(self):

root = TreeNode(3)
root.left = TreeNode(4)
root.right = TreeNode(5)

root.left.left = TreeNode(1)
root.left.right = TreeNode(3)

root.right.right = TreeNode(1)



return root


def createTree2(self):
root = TreeNode(3)

root.left = TreeNode(2)
root.right = TreeNode(3)

root.left.right = TreeNode(3)
root.right.right = TreeNode(1)

return root



# s = Solution2()
#
# root = s.createTree2()
#
# print s.rob(root)



# 刘宇波的代码1
# 递归版
class Solution3(object):
def rob(self, root):
"""
:type root: TreeNode
:rtype: int
"""


return self.tryRob(root,True)


def tryRob(self,root,include):

if not root:
return 0



res = self.tryRob(root.left,True) + self.tryRob(root.right,True)

if include:
res = max(res,root.val + self.tryRob(root.left,False) + self.tryRob(root.right,False))



return res

# 刘宇波的代码2# 这个include 技巧用的好呀, 不同的递归函数用一个参数就合并了class Solution4(object): def rob(self, root): """ :type root: TreeNode :rtype: int """ self.memo = {} return self.tryRob(root,True) def tryRob(self,root,include): if not root: return 0 if self.memo.has_key(root): return self.memo[root] res = self.tryRob(root.left,True) + self.tryRob(root.right,True) if include: res = max(res,root.val + self.tryRob(root.left,False) + self.tryRob(root.right,False)) self.memo[root] = res return res# 刘宇波的代码3# 二元返回值分别代表包含该节点的最大值和不包含该节点的最大值class Solution5(object): def rob(self, root): """ :type root: TreeNode :rtype: int """ res = self.tryRob(root) return res[1] # res[0] 不包含root 的最大值 # res[1] 包含root 或者不包含 root 的最大值 def tryRob(self,root): if not root: return [0,0] resL = self.tryRob(root.left) resR = self.tryRob(root.right) res=[0,0] res[0] = resL[1] + resR[1] res[1] = max(res[0],root.val + resL[0] + resR[0]) return res

猜你喜欢

转载自www.cnblogs.com/lux-ace/p/10546622.html