Leetcode no. 337
来源:互联网 发布:ae软件要钱吗 编辑:程序博客网 时间:2024/05/18 03:19
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 1Maximum amount of money the thief can rob = 3 + 3 + 1 = 7.
Example 2:
3 / \ 4 5 / \ \ 1 3 1Maximum amount of money the thief can rob = 4 + 5 = 9.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int rob(TreeNode root){ int[] res= robRec(root); return Math.max(res[0], res[1]); } private int[] robRec(TreeNode n){ if (n==null) return new int[2]; int[] left= robRec(n.left); int[] right= robRec(n.right); int[] res= new int[2]; res[0]= n.val+left[1]+right[1]; res[1]= Math.max(left[0],left[1])+Math.max(right[0],right[1]); return res; }}
0 0
- Leetcode no. 337
- LeetCode No.337 House Robber III
- LeetCode No.11 ~No.12
- Leetcode NO.78 Subsets
- Leetcode NO.77 Combinations
- Leetcode NO.46 Permutations
- 【leetcode】No. 235 LCABST
- LeetCode no. 35
- Leetcode no. 37
- Leetcode no. 279
- Leetcode no. 335
- Leetcode no. 283
- Leetcode no. 181 (sql)
- Leetcode no. 40
- Leetcode no. 238
- Leetcode no. 26
- Leetcode no. 80
- Leetcode no. 228
- 编写安全的C代码
- sdau-2 1017
- 智力题
- Android chromium 和 Android Webkit 区别
- css3新增的文本高级样式-文本阴影和文本溢出
- Leetcode no. 337
- list.sort()
- acm 2 1026 Sudoku Killer
- 嵌入式Linux 驱动开发头文件 定时器寄存器宏定义头文件
- HDU 1248 寒冰王座(完全背包)(dp 或 暴力)
- 简单的fragment传值
- 练习二 1011 石油田的问题
- Leetcode no. 70
- android 分辨率自适应