CODE 21: Path Sum
来源:互联网 发布:手机知乎怎么上传图片 编辑:程序博客网 时间:2024/05/17 00:05
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.
public boolean hasPathSum(TreeNode root, int sum) {// Start typing your Java solution below// DO NOT write main() functionif (null == root) {return false;}return dfs(root, 0, sum);}private boolean dfs(TreeNode root, int current, int sum) {if (root.left == null && root.right == null) {if (current + root.val == sum) {return true;} else {return false;}}boolean is = false;if (root.left != null) {is = dfs(root.left, current + root.val, sum);}if (is) {return is;}if (root.right != null) {is = dfs(root.right, current + root.val, sum);}return is;}
- CODE 21: Path Sum
- [leet code] Path Sum
- path-sum Java code
- path-sum Java code
- CODE 20: Path Sum II
- CODE 69: Minimum Path Sum
- [leet code] Minimum Path Sum
- [leet code] Path Sum II
- CODE 9: Binary Tree Maximum Path Sum
- leetcode 日经贴,Cpp code -Path Sum II
- leetcode 日经贴,Cpp code -Path Sum
- 【Leet Code】64. Minimum Path Sum---Medium
- binary-tree-maximum-path-sum Java code
- Path Sum && Path Sum ||
- Binary Tree Maximum Path Sum[leet code test cases passed]
- craking the code interview all path sum python
- leetcode 日经贴,Cpp code -Binary Tree Maximum Path Sum
- Leet Code OJ 112. Path Sum [Difficulty: Easy]
- Android 百度地图开发(三)--- 实现比例尺功能和替换自带的缩放组件
- hdu 3571 N-dimensional Sphere 高斯消元
- android文件操作(基本囊括所有的文件操作了)
- 网站安全之XSS漏洞攻击以及防范措施
- 对短信群发行业的简单描述
- CODE 21: Path Sum
- 使用CMFCOutlookBarPane创建分组控件,禁用双击编辑的功能
- 关于Oracle 释放过度使用的Undo表空间
- switch语句作用的类型
- C++中的istringstream
- [转]深入Java单例模式
- java定时器的使用
- C#解决TabControl 页里面多余边距问题
- 在此写下最近做电商网页设计找到的电商网站,电商网站有那些电商网站地址网上购网站有那些,网上买东西的网站有那些,