【LeetCode】Path Sum
来源:互联网 发布:python是面向对象吗 编辑:程序博客网 时间:2024/06/05 17:29
Path Sum
Total Accepted: 5082 Total Submissions: 17224 My Submissions
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 1return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
用的bfs,AC以后一想,其实dfs也可以做。
dfs和bfs都试了一下,发现还是bfs稍微快一些,快了60ms。
搜索路径吧,然后比较和。
Java AC BFS
/** * 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;} return (bfs(root, sum)); } public boolean bfs(TreeNode root, int sum){ Queue<NodeSum> nodeQueue = new LinkedList<NodeSum>(); nodeQueue.offer(new NodeSum(root,root.val)); while(!nodeQueue.isEmpty()){ NodeSum node = nodeQueue.peek(); nodeQueue.poll(); if(node.node.left == null && node.node.right == null && node.sum == sum){ return true; } TreeNode point = node.node; int newSum = node.sum; if(point.left != null){ point = point.left; nodeQueue.offer(new NodeSum(point,(newSum + point.val))); } point = node.node; if(point.right != null){ point = point.right; nodeQueue.offer(new NodeSum(point,(newSum + point.val))); } } return false; } public class NodeSum { int sum ; TreeNode node; NodeSum(TreeNode root ,int sum){ super(); this.node = root; this.sum = sum; } }}
Java AC DFS
/** * 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;} return (dfs(root, sum , root.val)); } public boolean dfs(TreeNode root, int sum , int allSum){ if (root.left == null && root.right == null ) {if (allSum == sum) {return true;}} TreeNode point = root; if (point.left != null) { point = point.left;if (dfs(point, sum, allSum + point.val)) {return true;}} point = root; if (point.right != null) { point = point.right; if (dfs(point, sum, allSum + point.val)) {return true;}} return false; } public class NodeSum { int sum ; TreeNode node; NodeSum(TreeNode root ,int sum){ super(); this.node = root; this.sum = sum; } }}
0 0
- leetcode Path Sum && Path Sum ||
- 【Leetcode】Path Sum (Sum)
- leetcode:leetcode :Path Sum
- 【Leetcode】Path Sum II (Sum)
- LeetCode: Minimum Path Sum
- LeetCode: Path Sum
- LeetCode: Path Sum II
- LeetCode Minimum Path Sum
- LeetCode Path Sum
- LeetCode Path Sum II
- [Leetcode] Path Sum
- [Leetcode] Path Sum II
- LeetCode : Minimum Path Sum
- [Leetcode] Minimum Path Sum
- LeetCode: Path Sum
- LeetCode: Path Sum II
- [LeetCode] Path Sum
- [LeetCode] Path Sum II
- 随写
- 路由器题目
- IOS访问限制密码忘记解决方案
- 在Eclipse中使用JUnit4进行单元测试(高级篇)
- java连接linux中mysql出现:Access denied for user 'root'@'192.168.91.1' (using password: YES)
- 【LeetCode】Path Sum
- ****有关onActivityResult提前调用的问题
- 重定义NSLog
- struts2.1.8启动 FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION
- 学习鸟哥的Linux私房菜笔记(10)——bash2
- onCreateOptionsMenu的作用
- js时间戳转为日期格式
- 解决sqlserver2008在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQ
- 正则表达式匹配方式