112. Path Sum
来源:互联网 发布:风人之戒,不可慎乎 编辑:程序博客网 时间:2024/06/05 17:21
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example: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.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) { return false; } if (root.left == null && root.right == null && sum - root.val == 0) { return true; } return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); }}
0 0
- 112. Path Sum && 113. Path Sum II
- 112. Path Sum&113. Path Sum II
- 112.Path Sum
- LeetCode 112. Path Sum
- [LeetCode]112.Path Sum
- 【LeetCode】112.Path Sum
- [Leetcode] 112. Path Sum
- [leetcode] 112.Path Sum
- 112.Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum LeetCode
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 在CentOS6.8下安装Docker
- JavaScript对象(Math)
- 02-JZ2440裸机学习之GPIO实验
- 关于java中的OutOfMemory种类和解决方法
- 基于Mycat的数据库切分方案和Join探索
- 112. Path Sum
- 蓝桥-图5-广度优先遍历-转机最少
- 浏览器分别在哪些情况下使用get方式和post方式访问web服务器
- 栈的应用(二)
- 【Oracle数据库】分组函数
- Ubuntu 14.04 搭建Nexus Maven 私服
- Sublime Text安装Emmet(Mac)
- wifi网络接入原理(上)——扫描Scanning
- java常用api