-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_3249.java
113 lines (105 loc) · 4.05 KB
/
_3249.java
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.fishercoder.solutions.fourththousand;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class _3249 {
public static class Solution1 {
/*
* My completely original solution during the contest.
*/
class TreeNode {
int val;
List<TreeNode> children;
int totalChildrenCount;
public TreeNode(int val) {
this.val = val;
this.children = new ArrayList<>();
this.totalChildrenCount = 1; // count itself as its own child
}
}
int goodNodes = 0;
public int countGoodNodes(int[][] edges) {
if (edges == null || edges.length == 0 || edges[0].length == 0) {
return 0;
}
TreeNode root = buildTree(edges);
postOrder(root);
dfs(root);
return goodNodes;
}
private void dfs(TreeNode root) {
if (root == null || root.children.isEmpty()) {
return;
}
int size = root.children.get(0).totalChildrenCount;
if (size == 0) {
return;
}
boolean possiblyGoodNode = true;
for (TreeNode child : root.children) {
if (child.totalChildrenCount != size) {
possiblyGoodNode = false;
break;
}
}
if (possiblyGoodNode) {
goodNodes++;
}
for (TreeNode child : root.children) {
dfs(child);
}
}
private int postOrder(TreeNode root) {
if (root == null) {
return 0;
}
if (root.children.isEmpty()) {
goodNodes++;
return 1;
}
int totalChildrenCount = 1;
for (TreeNode child : root.children) {
int count = postOrder(child);
totalChildrenCount += count;
}
root.totalChildrenCount = totalChildrenCount;
return totalChildrenCount;
}
private TreeNode buildTree(int[][] edges) {
Map<Integer, TreeNode> map = new HashMap<>();
for (int i = 0; i < edges.length; i++) {
if (edges[i][0] == 0 || edges[i][1] == 0) {
if (edges[i][0] == 0) {
TreeNode parent = map.getOrDefault(edges[i][0], new TreeNode(edges[i][0]));
TreeNode child = map.getOrDefault(edges[i][1], new TreeNode(edges[i][1]));
parent.children.add(child);
map.put(edges[i][0], parent);
map.put(edges[i][1], child);
} else {
TreeNode parent = map.getOrDefault(edges[i][1], new TreeNode(edges[i][1]));
TreeNode child = map.getOrDefault(edges[i][0], new TreeNode(edges[i][0]));
parent.children.add(child);
map.put(edges[i][1], parent);
map.put(edges[i][0], child);
}
} else {
if (map.containsKey(edges[i][0])) {
TreeNode parent = map.get(edges[i][0]);
TreeNode child = map.getOrDefault(edges[i][1], new TreeNode(edges[i][1]));
parent.children.add(child);
map.put(edges[i][0], parent);
map.put(edges[i][1], child);
} else if (map.containsKey(edges[i][1])) {
TreeNode parent = map.get(edges[i][1]);
TreeNode child = map.getOrDefault(edges[i][0], new TreeNode(edges[i][0]));
parent.children.add(child);
map.put(edges[i][1], parent);
map.put(edges[i][0], child);
}
}
}
return map.get(0);
}
}
}