GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — LeetCode/L112_Path_Sum.java at master · likevson/LeetCode Expand file tree/ Copy pathL112_Path_Sum.javaMore file actions 53 lines (37 loc) · 898 Bytes / Copy pathL112_Path_Sum.javaFile metadata and controls53 lines (37 loc) · 898 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 package LeetCode; public class L112_Path_Sum { boolean hasPath; public boolean hasPathSum(TreeNode root, int sum) { if (root == null) { return false; } hasPath = false; help(root, 0, sum); return hasPath; } void help(TreeNode node, int cur, int sum) { cur += node.val; boolean isLeaf = (node.left == null) && (node.right == null); if (cur == sum && isLeaf) { hasPath = true; } if (node.left != null) { help(node.left, cur, sum); } if (node.right != null) { help(node.right, cur, sum); } cur -= node.val; } public boolean hasPathSum2(TreeNode root, int sum) { if (root == null) { return false; } if (root.left == null && root.right == null) { return root.val == sum; } return (root.left != null && hasPathSum2(root.left, sum - root.val)) || (root.right != null && hasPathSum2(root.right, sum - root.val)); } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |