House Robber III
2016-07-24 02:35
337 查看
/** * 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) { if (root == null) { return 0; } int[] res = helper(root); return Math.max(res[0], res[1]); } private int[] helper(TreeNode node) { if (node == null) { return new int[]{0, 0}; } int[] left = helper(node.left); int[] right = helper(node.right); int[] res = new int[2]; res[0] = node.val + left[1] + right[1]; res[1] = Math.max(left[0], left[1]) + Math.max(right[0], right[1]); return res; } }
相关文章推荐
- House Robber II
- House Robber
- 晨魅--初学Linux系统
- 深入理解java反射机制
- FastDFS Tracker-Leader选择
- Windows/Linux服务器/Git/svn/get和post
- 三大原理(计算机原理、操作系统原理、编译原理)两个协议(TCP与HTTP协议)一种结构(数据结构)
- BZOJ3257 : 树的难题
- [React Native] State and Touch Events -- TextInput, TouchableHighLight
- C++ exception类
- Bellovin
- Shader,想说爱你不容易。
- 现在看看自己写的博客,怎么感觉好low啊。。。
- 逐步深入TCP/IP协议栈
- 平台化项目多语言架构实现
- asp.net显示用户信息
- C++ throw:抛出自己的异常
- 关于win10安装ubuntu16.04
- Aaronson
- C++ char[] 与 string 转换