O(n) cpp. total even sized childs


#1

Calculate the number of even sized child.

int mm(map<int,vector> &m, int index, int &an)
{
int ans = 0;
if(m[index].size()==0)
return 0;

for(int i = 0 ;i < m[index].size();i++)
{
    ans = 1 + mm(m,m[index][i], an);    
    if(ans%2 == 0)
    an++;
}
return ans;

}

int Solution::solve(int A, vector<vector > &B) {

map<int,vector<int>> m;

for(int i = 0;i<B.size();i++)
{
    m[B[i][0]].push_back(B[i][1]);
}

int ans = 0;
mm(m,1, ans);
return ans;

}