From 1cf1440ac3c9664d20779f3dba299508131fba8e Mon Sep 17 00:00:00 2001 From: JoannaCode Date: Thu, 24 Mar 2016 20:43:28 -0700 Subject: [PATCH] finished 112 --- Easy/PathSum_112.java | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Easy/PathSum_112.java diff --git a/Easy/PathSum_112.java b/Easy/PathSum_112.java new file mode 100644 index 00000000..e573dcc9 --- /dev/null +++ b/Easy/PathSum_112.java @@ -0,0 +1,44 @@ +/** + * Path Sum + * 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 + * + * Tags: Tree Depth-first Search + * Similar Problems: (M) Path Sum II (H) Binary Tree Maximum Path Sum (M) Sum Root to Leaf Numbers + * @author:chenshuna + */ + +public class PathSum_112 { + public static boolean hasPathSum(TreeNode root, int sum) { + if(root == null){ + return false; + } + if(sum == root.val && root.left == null && root.right == null){ + return true; + } + if(root != null){ + return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); + } + return false; + } + + public static void main(String[] args) { + TreeNode res = new TreeNode(5); + res.left = new TreeNode(4); + res.right = new TreeNode(8); + res.left.left = new TreeNode(11); + res.right.left = new TreeNode(13); + res.right.right = new TreeNode(4); + res.left.left.left= new TreeNode(7); + res.left.left.right= new TreeNode(2); + System.out.print(hasPathSum(res, 22)); + } +} \ No newline at end of file