LeetCode--No.112--Path Sum
来源:互联网 发布:macbook12寸装windows 编辑:程序博客网 时间:2024/05/08 09:28
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; else return equal(root, sum); } public boolean equal (TreeNode node, int sum){ if (node.left == null && node.right == null){ if(node.val == sum) return true; else return false; } else if (node.left == null){ if (equal(node.right, sum-node.val)) return true; else return false; } else if(node.right == null){ if (equal(node.left, sum-node.val)) return true; else return false; } else{ if (equal(node.left, sum-node.val) || equal(node.right, sum-node.val)) return true; else return false; } }}只是。。。这TM也叫代码?!!这么恶心?!!这是我写的?!!食屎去吧!!!!
public class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right, sum-root.val); }}这才叫代码好嘛!
非递归(队列)解法:
/** * 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; LinkedList<TreeNode> nodes = new LinkedList<TreeNode>(); LinkedList<Integer> values = new LinkedList<Integer>(); nodes.add(root); values.add(root.val); while(!nodes.isEmpty()){ TreeNode curr = nodes.poll(); int sumValue = values.poll(); if(curr.left == null && curr.right == null && sumValue == sum) return true; if(curr.left != null){ nodes.add(curr.left); values.add(sumValue + curr.left.val); } if(curr.right != null){ nodes.add(curr.right); values.add(sumValue + curr.right.val); } } return false; }}
代码来自:http://www.programcreek.com/2013/01/leetcode-path-sum/
0 0
- Leetcode NO.112 Path Sum
- LeetCode--No.112--Path Sum
- LeetCode No.112 Path Sum
- Leetcode NO.113 Path Sum II
- LeetCode No.437 Path Sum III
- LeetCode(112)Path Sum
- LeetCode 112 Path Sum
- LeetCode: Path Sum [112]
- LeetCode 112 Path Sum
- [leetcode 112] Path Sum
- leetCode #112 path sum
- [LeetCode 112]Path Sum
- leetcode || 112、Path Sum
- LeetCode 112: Path Sum
- LeetCode 112 Path Sum
- leetcode[112]:Path Sum
- leetcode-112-Path Sum
- Leetcode# 112 Path Sum
- 操作系统之进程与线程
- 关于android studio 运行微博官方的 weibosdkdemo 所出现的问题及解决方法整理
- opencv中的膨胀与腐蚀
- LeetCode 62. Unique Paths
- Ipython Notebook升级版Jupyter Notebook远程服务器配置
- LeetCode--No.112--Path Sum
- Laravel 课程自学笔记
- HTML5学习笔记
- 关于离线浏览器的记录
- 基于链式存储结构的线性表
- 登录注册模块、状态保持模块
- 个人信息修改模块
- 主页切换模块,包括侧滑
- 网络请求