import
java.util.*;
class
Node {
int
data;
Node left;
Node right;
Node(
int
data) {
this
.data = data;
left =
null
;
right =
null
;
}
}
public
class
Main {
public
static
void
main(String[] args) {
Node root =
new
Node(
2
);
root.left =
new
Node(
1
);
root.right =
new
Node(
4
);
root.left.left =
new
Node(
2
);
root.left.right =
new
Node(
1
);
root.right.right =
new
Node(
1
);
root.right.right.left =
new
Node(
7
);
root.right.right.right =
new
Node(
5
);
List<Integer> uniqueNodes = findUniquePattern(root);
System.out.println(uniqueNodes.get(
0
) +
" "
+
uniqueNodes.get(
1
) +
" "
+
uniqueNodes.get(
2
));
}
public
static
List<Integer> findUniquePattern(Node root) {
Map<Integer, Integer> twoChild =
new
HashMap<>();
Map<Integer, Integer> oneChild =
new
HashMap<>();
Map<Integer, Integer> noChild =
new
HashMap<>();
trackPattern(root, twoChild, oneChild, noChild);
return
Arrays.asList(noChild.size(), oneChild.size(), twoChild.size());
}
public
static
void
trackPattern(Node root, Map<Integer, Integer> twoChild,
Map<Integer, Integer> oneChild,
Map<Integer, Integer> noChild) {
if
(root ==
null
) {
return
;
}
if
(root.left !=
null
&& root.right !=
null
) {
twoChild.put(root.data, twoChild.getOrDefault(root.data,
0
) +
1
);
trackPattern(root.left, twoChild, oneChild, noChild);
trackPattern(root.right, twoChild, oneChild, noChild);
}
else
if
(root.left ==
null
&& root.right ==
null
) {
noChild.put(root.data, noChild.getOrDefault(root.data,
0
) +
1
);
}
else
{
oneChild.put(root.data, oneChild.getOrDefault(root.data,
0
) +
1
);
if
(root.left !=
null
) {
trackPattern(root.left, twoChild, oneChild, noChild);
}
if
(root.right !=
null
) {
trackPattern(root.right, twoChild, oneChild, noChild);
}
}
}
}