Path Sum
来源:互联网 发布:淘宝多少好评一个心 编辑:程序博客网 时间:2024/05/22 00:36
1.题目
判断是否从根到叶子的路径和跟给定sum相同的
Given the below binary tree and sum = 22
,
5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
2.算法
用递归法,看看左子树或右子树有没有满足条件的路径
public boolean hasPathSum(TreeNode root, int sum) { if(root == null) return false; if(root.left == null && root.right==null && root.val==sum) return true; return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val); }
0 0
- Path Sum && Path Sum ||
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Path Sum
- Android emoji显示
- 每天一个linux命令(5):rm 命令
- mysql主从同步原理
- 我的 2016
- 2017.01.03:数组和字符串
- Path Sum
- MySQL——InnoDB锁问题(四)
- 在Android中学会使用SVG图片
- 浅谈Android事件总线框架EventBus
- mimetype解释及检索
- python pip安装 镜像资源
- 设计模式的六大原则
- Mysql Err:1366, u"Incorrect string value
- CCS中时间统计方法(profile)