GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/337_House_Robber_III.py at master · qiyuangong/leetcode Expand file tree/ Copy path337_House_Robber_III.pyMore file actions 49 lines (46 loc) · 1.51 KB / Copy path337_House_Robber_III.pyFile metadata and controls49 lines (46 loc) · 1.51 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): # def rob(self, root): # """ # :type root: TreeNode # :rtype: int # """ # dic = {} # return self.rob_helper(root, dic) # # def rob_helper(self, root, dic): # # recursion with hash map # if root is None: # return 0 # if root in dic: # return dic[root] # res = 0 # if root.left is not None: # res += self.rob_helper(root.left.left, dic) + self.rob_helper(root.left.right, dic) # if root.right is not None: # res += self.rob_helper(root.right.left, dic) + self.rob_helper(root.right.right, dic) # res = max(res + root.val, self.rob_helper(root.left, dic) + self.rob_helper(root.right, dic)) # dic[root] = res # return res def rob(self, root): """ :type root: TreeNode :rtype: int """ # res[0] means skip curr, res[1] means get curr res = self.rob_helper(root) return max(res[0], res[1]) def rob_helper(self, root): if root is None: return [0, 0] left = self.rob_helper(root.left) right = self.rob_helper(root.right) res = [0, 0] res[0] = max(left[0], left[1]) + max(right[0], right[1]) res[1] = root.val + left[0] + right[0] return res |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |