|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import com.fishercoder.common.classes.TreeNode; |
| 4 | + |
| 5 | +/** |
| 6 | + * 666. Path Sum IV |
| 7 | + * If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digits integers. |
| 8 | +
|
| 9 | + For each integer in this list: |
| 10 | +
|
| 11 | + The hundreds digit represents the depth D of this node, 1 <= D <= 4. |
| 12 | +
|
| 13 | + The tens digit represents the position P of this node in the level it belongs to, 1 <= P <= 8. |
| 14 | + The position is the same as that in a full binary tree. |
| 15 | +
|
| 16 | + The units digit represents the value V of this node, 0 <= V <= 9. |
| 17 | +
|
| 18 | + Given a list of ascending three-digits integers representing a binary with the depth smaller than 5. |
| 19 | + You need to return the sum of all paths from the root towards the leaves. |
| 20 | +
|
| 21 | + Example 1: |
| 22 | +
|
| 23 | + Input: [113, 215, 221] |
| 24 | + Output: 12 |
| 25 | + Explanation: |
| 26 | + The tree that the list represents is: |
| 27 | + 3 |
| 28 | + / \ |
| 29 | + 5 1 |
| 30 | +
|
| 31 | + The path sum is (3 + 5) + (3 + 1) = 12. |
| 32 | +
|
| 33 | + Example 2: |
| 34 | +
|
| 35 | + Input: [113, 221] |
| 36 | + Output: 4 |
| 37 | + Explanation: |
| 38 | + The tree that the list represents is: |
| 39 | + 3 |
| 40 | + \ |
| 41 | + 1 |
| 42 | +
|
| 43 | + The path sum is (3 + 1) = 4. |
| 44 | +
|
| 45 | + */ |
| 46 | +public class _666 { |
| 47 | + public static class Solution1 { |
| 48 | + /**OMG, since it's no larger than depth 5, I've got a hardcoded solution here.... |
| 49 | + * By "harcoded", I mean the constructTree() method.*/ |
| 50 | + public int totalSum = 0; |
| 51 | + |
| 52 | + public int pathSum(int[] nums) { |
| 53 | + TreeNode root = constructTree(nums); |
| 54 | + if (root == null) { |
| 55 | + return 0; |
| 56 | + } |
| 57 | + computePathSum(root, 0); |
| 58 | + return totalSum; |
| 59 | + } |
| 60 | + |
| 61 | + private void computePathSum(TreeNode root, int pathSum) { |
| 62 | + pathSum += root.val; |
| 63 | + if (root.left != null) { |
| 64 | + computePathSum(root.left, pathSum); |
| 65 | + } |
| 66 | + if (root.right != null) { |
| 67 | + computePathSum(root.right, pathSum); |
| 68 | + } |
| 69 | + if (root.left == null && root.right == null) { |
| 70 | + totalSum += pathSum; |
| 71 | + } |
| 72 | +// pathSum -= root.val; |
| 73 | + /**this line is not necessary as I'm passing pathSum as a local variable around, so it's always updated |
| 74 | + it's AC'ed with or without this line*/ |
| 75 | + } |
| 76 | + |
| 77 | + private TreeNode constructTree(int[] nums) { |
| 78 | + if (nums == null || nums.length == 0) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + TreeNode root = new TreeNode(Integer.parseInt(Integer.toString(nums[0]).substring(2, 3))); |
| 82 | + //depth 2 |
| 83 | + for (int i = 1; i < nums.length; i++) { |
| 84 | + if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 2 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) { |
| 85 | + root.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 86 | + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 2 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) { |
| 87 | + root.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + //depth 3 |
| 92 | + for (int i = 2; i < nums.length; i++) { |
| 93 | + if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) { |
| 94 | + root.left.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 95 | + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) { |
| 96 | + root.left.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 97 | + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 3) { |
| 98 | + root.right.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 99 | + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 3 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 4) { |
| 100 | + root.right.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + //depth 4 |
| 105 | + for (int i = 3; i < nums.length; i++) { |
| 106 | + if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 1) { |
| 107 | + root.left.left.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 108 | + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 2) { |
| 109 | + root.left.left.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 110 | + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 3) { |
| 111 | + root.left.right.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 112 | + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 4) { |
| 113 | + root.left.right.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 114 | + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 5) { |
| 115 | + root.right.left.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 116 | + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 6) { |
| 117 | + root.right.left.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 118 | + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 7) { |
| 119 | + root.right.right.left = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 120 | + } else if (Integer.parseInt(Integer.toString(nums[i]).substring(0, 1)) == 4 && Integer.parseInt(Integer.toString(nums[i]).substring(1, 2)) == 8) { |
| 121 | + root.right.right.right = new TreeNode(Integer.parseInt(Integer.toString(nums[i]).substring(2, 3))); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return root; |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments