#include <bits/stdc++.h>
using
namespace
std;
class
Node {
public
:
vector<Node*> children;
int
val;
Node(
int
v)
{
val = v;
children = {};
}
};
int
postOrder(Node* root,
int
count[])
{
if
(root->children.size() == 0)
return
1;
int
height = 0;
for
(Node* child : root->children)
{
int
h = postOrder(child, count);
if
(height == 0)
height = h;
else
if
(h == -1 || height != h)
height = -1;
}
if
(height != -1)
count[0]++;
return
height != -1 ? height + 1 : -1;
}
int
equalHeightBranches(Node* root)
{
if
(root == NULL)
return
0;
int
count[1] = { 0 };
postOrder(root, count);
return
count[0];
}
int
main()
{
Node* seven =
new
Node(7);
Node* seven2 =
new
Node(7);
Node* five =
new
Node(5);
Node* four =
new
Node(4);
Node* nine =
new
Node(9);
Node* one =
new
Node(1);
Node* two =
new
Node(2);
Node* six =
new
Node(6);
Node* eight =
new
Node(8);
Node* ten =
new
Node(10);
Node* three =
new
Node(3);
Node* mfour =
new
Node(-4);
Node* mtwo =
new
Node(-2);
Node* zero =
new
Node(0);
three->children.push_back(mfour);
three->children.push_back(mtwo);
three->children.push_back(zero);
ten->children.push_back(three);
two->children.push_back(six);
two->children.push_back(seven2);
four->children.push_back(nine);
four->children.push_back(one);
four->children.push_back(five);
seven->children.push_back(ten);
seven->children.push_back(two);
seven->children.push_back(eight);
seven->children.push_back(four);
cout << (equalHeightBranches(seven));
}