path-sum Java code
来源:互联网 发布:如何取消淘宝店铺差评 编辑:程序博客网 时间:2024/05/17 05:16
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 andsum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.
/** * Definition for binary tree * 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; int now=0; return digui(root,sum,now); } boolean digui(TreeNode root, int sum,int now){ now+=root.val; if(sum==now&&root.left==null&&root.right==null){ return true; } boolean left=false,right=false; if(root.left!=null) left= digui(root.left,sum,now); if(root.right!=null) right= digui(root.right,sum,now); return left||right; }}
阅读全文
0 0
- path-sum Java code
- path-sum Java code
- binary-tree-maximum-path-sum Java code
- CODE 21: Path Sum
- [leet code] Path Sum
- CODE 20: Path Sum II
- CODE 69: Minimum Path Sum
- [leet code] Minimum Path Sum
- [leet code] Path Sum II
- [Leetcode] Path Sum (Java)
- Path Sum II Java
- Minimum Path Sum Java
- Minimum Path Sum java
- [leetcode]Path Sum[java]
- Path Sum (Java)
- Path Sum II (Java)
- Minimum Path Sum (Java)
- [LeetCode][Java] Path Sum
- [超实用]eclipse快捷键大全
- Java
- git tag与git tag -a的不同
- “北京时间”全站HTTPS加密,引领新闻网站加密趋势
- 设计模式--责任链模式
- path-sum Java code
- OpenCV实现彩色点的实时追踪
- LeetCode-104. Maximum Depth of Binary Tree
- 先到先服务(FCFS)算法C语言,Java语言实现
- OpenCV直方图计算,以及颜色迁移
- QGis二次开发基础 -- 添加矢量要素功能
- 【二分法】LeetCode-Search Insert Position
- 拉格朗日对偶
- activity与fragment的区别