Leetcode 337: House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.

Determine the maximum amount of money the thief can rob tonight without alerting the police.

Example 1:

     3
    / \
   2   3
    \   \ 
     3   1

Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.

 

Example 2:

     3
    / \
   4   5
  / \   \ 
 1   3   1

Maximum amount of money the thief can rob = 4 + 5 = 9.

 

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left;
 6  *     public TreeNode right;
 7  *     public TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     private Dictionary<Tuple<TreeNode, bool>, int> cache = new Dictionary<Tuple<TreeNode, bool>, int>();
12     
13     public int Rob(TreeNode root) {
14         if (root == null) return 0;
15         return Math.Max(DFS(root, true), DFS(root, false));
16     }
17     
18     private int DFS(TreeNode node, bool canRob)
19     {
20         if (node == null) return 0;
21         
22         var key = new Tuple<TreeNode, bool>(node, canRob);
23         
24         if (cache.ContainsKey(key))
25         {
26             return cache[key];
27         }
28         
29         var result = 0;
30           
31         if (!canRob)
32         {
33             result = DFS(node.left, true) + DFS(node.right, true);    
34         }
35         else
36         {
37             result = Math.Max(node.val + DFS(node.left, false) + DFS(node.right, false), DFS(node.left, true) + DFS(node.right, true));
38         }
39         
40         cache[key] = result;
41         
42         return result;
43     }
44 }

 

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left;
 6  *     public TreeNode right;
 7  *     public TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public int Rob(TreeNode root) {
12         var result = DFS(root);
13         return Math.Max(result[0], result[1]);
14     }
15     
16     private int[] DFS(TreeNode node)
17     {
18         if (node == null) return new int[2];
19         
20         var left = DFS(node.left);
21         var right = DFS(node.right);
22         
23         var result = new int[2];
24         
25         // result[0] means don't rob current node
26         // result[1] means rob current node
27         result[0] = Math.Max(left[0], left[1]) + Math.Max(right[0], right[1]);
28         result[1] = node.val + left[0] + right[0];
29         
30         return result;
31     }
32 }
33 
34 /**
35  * Definition for a binary tree node.
36  * public class TreeNode {
37  *     public int val;
38  *     public TreeNode left;
39  *     public TreeNode right;
40  *     public TreeNode(int x) { val = x; }
41  * }
42  */
43 public class Solution1 {
44     private Dictionary<Tuple<TreeNode, bool>, int> cache = new Dictionary<Tuple<TreeNode, bool>, int>();
45     
46     public int Rob(TreeNode root) {
47         if (root == null) return 0;
48         return Math.Max(DFS(root, true), DFS(root, false));
49     }
50     
51     private int DFS(TreeNode node, bool canRob)
52     {
53         if (node == null) return 0;
54         
55         var key = new Tuple<TreeNode, bool>(node, canRob);
56         
57         if (cache.ContainsKey(key))
58         {
59             return cache[key];
60         }
61         
62         var result = 0;
63           
64         if (!canRob)
65         {
66             result = DFS(node.left, true) + DFS(node.right, true);    
67         }
68         else
69         {
70             result = Math.Max(node.val + DFS(node.left, false) + DFS(node.right, false), DFS(node.left, true) + DFS(node.right, true));
71         }
72         
73         cache[key] = result;
74         
75         return result;
76     }
77 }

 

posted @ 2017-12-12 06:33  逸朵  阅读(147)  评论(0)    收藏  举报