【LeetCode】Path Sum
2014-01-08 14:10
330 查看
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
return 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
Java AC DFS
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 1
return 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; } } }
相关文章推荐
- LeetCode | Path Sum
- leetcode 64. Minimum Path Sum
- [leetcode] Path Sum
- LeetCode *** 112. Path Sum
- LeetCode 64. Minimum Path Sum
- LeetCode 64. Minimum Path Sum
- [leetcode]Path Sum
- leetCode刷题归纳-Dynamic Programming(64. Minimum Path Sum)
- [LeetCode116]Path Sum
- LeetCode 64. Minimum Path Sum
- LeetCode112 Path Sum
- leetcode - 62,63. Unique Paths(II) & 64.Minimum Path Sum
- [leetcode]Path Sum
- LEETCODE: Path Sum
- 【leetcode c++】 112 Path Sum
- LeetCode学习篇二十三——Path Sum
- 【leetcode】64. Minimum Path Sum
- LeetCode 64. Minimum Path Sum
- LeetCode-Easy刷题(26) Path Sum
- LeetCode:Path Sum