|
| 1 | +packagecom.fishercoder.solutions.fourththousand; |
| 2 | + |
| 3 | +importjava.util.ArrayList; |
| 4 | +importjava.util.HashMap; |
| 5 | +importjava.util.List; |
| 6 | +importjava.util.Map; |
| 7 | + |
| 8 | +publicclass_3249 { |
| 9 | +publicstaticclassSolution1 { |
| 10 | +/** |
| 11 | + * My completely original solution during the contest. |
| 12 | + */ |
| 13 | +classTreeNode { |
| 14 | +intval; |
| 15 | +List<TreeNode>children; |
| 16 | +inttotalChildrenCount; |
| 17 | + |
| 18 | +publicTreeNode(intval) { |
| 19 | +this.val =val; |
| 20 | +this.children =newArrayList<>(); |
| 21 | +this.totalChildrenCount =1;//count itself as its own child |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | +intgoodNodes =0; |
| 26 | + |
| 27 | +publicintcountGoodNodes(int[][]edges) { |
| 28 | +if (edges ==null ||edges.length ==0 ||edges[0].length ==0) { |
| 29 | +return0; |
| 30 | + } |
| 31 | +TreeNoderoot =buildTree(edges); |
| 32 | +postOrder(root); |
| 33 | +dfs(root); |
| 34 | +returngoodNodes; |
| 35 | + } |
| 36 | + |
| 37 | +privatevoiddfs(TreeNoderoot) { |
| 38 | +if (root ==null ||root.children.isEmpty()) { |
| 39 | +return; |
| 40 | + } |
| 41 | +intsize =root.children.get(0).totalChildrenCount; |
| 42 | +if (size ==0) { |
| 43 | +return; |
| 44 | + } |
| 45 | +booleanpossiblyGoodNode =true; |
| 46 | +for (TreeNodechild :root.children) { |
| 47 | +if (child.totalChildrenCount !=size) { |
| 48 | +possiblyGoodNode =false; |
| 49 | +break; |
| 50 | + } |
| 51 | + } |
| 52 | +if (possiblyGoodNode) { |
| 53 | +goodNodes++; |
| 54 | + } |
| 55 | +for (TreeNodechild :root.children) { |
| 56 | +dfs(child); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | +privateintpostOrder(TreeNoderoot) { |
| 61 | +if (root ==null) { |
| 62 | +return0; |
| 63 | + } |
| 64 | +if (root.children.isEmpty()) { |
| 65 | +goodNodes++; |
| 66 | +return1; |
| 67 | + } |
| 68 | +inttotalChildrenCount =1; |
| 69 | +for (TreeNodechild :root.children) { |
| 70 | +intcount =postOrder(child); |
| 71 | +totalChildrenCount +=count; |
| 72 | + } |
| 73 | +root.totalChildrenCount =totalChildrenCount; |
| 74 | +returntotalChildrenCount; |
| 75 | + } |
| 76 | + |
| 77 | +privateTreeNodebuildTree(int[][]edges) { |
| 78 | +Map<Integer,TreeNode>map =newHashMap<>(); |
| 79 | +for (inti =0;i <edges.length;i++) { |
| 80 | +if (edges[i][0] ==0 ||edges[i][1] ==0) { |
| 81 | +if (edges[i][0] ==0) { |
| 82 | +TreeNodeparent =map.getOrDefault(edges[i][0],newTreeNode(edges[i][0])); |
| 83 | +TreeNodechild =map.getOrDefault(edges[i][1],newTreeNode(edges[i][1])); |
| 84 | +parent.children.add(child); |
| 85 | +map.put(edges[i][0],parent); |
| 86 | +map.put(edges[i][1],child); |
| 87 | + }else { |
| 88 | +TreeNodeparent =map.getOrDefault(edges[i][1],newTreeNode(edges[i][1])); |
| 89 | +TreeNodechild =map.getOrDefault(edges[i][0],newTreeNode(edges[i][0])); |
| 90 | +parent.children.add(child); |
| 91 | +map.put(edges[i][1],parent); |
| 92 | +map.put(edges[i][0],child); |
| 93 | + } |
| 94 | + }else { |
| 95 | +if (map.containsKey(edges[i][0])) { |
| 96 | +TreeNodeparent =map.get(edges[i][0]); |
| 97 | +TreeNodechild =map.getOrDefault(edges[i][1],newTreeNode(edges[i][1])); |
| 98 | +parent.children.add(child); |
| 99 | +map.put(edges[i][0],parent); |
| 100 | +map.put(edges[i][1],child); |
| 101 | + }elseif (map.containsKey(edges[i][1])) { |
| 102 | +TreeNodeparent =map.get(edges[i][1]); |
| 103 | +TreeNodechild =map.getOrDefault(edges[i][0],newTreeNode(edges[i][0])); |
| 104 | +parent.children.add(child); |
| 105 | +map.put(edges[i][1],parent); |
| 106 | +map.put(edges[i][0],child); |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +returnmap.get(0); |
| 111 | + } |
| 112 | + } |
| 113 | +} |