CPP Even Tree Solution based on DFS


#1

The soltuion is based on dfs and same as question even tree of hackerrank

int res;
int sz;
int dfs(vector<int> arr[],bool vis[],int cn){
    int count =0;
    vis[cn]=true;
    // cout<<cn<<" :";
    for(int i =0; i<arr[cn].size(); i++){
        int chld=arr[cn][i];
        if(!vis[chld]){
            int x=dfs(arr,vis,chld);
            if(x%2==0)
                res++;
            else
                count+=x;
        }
    }
    return count+1;
}
int Solution::solve(int A, vector<vector<int> > &B) {
    res=0;
    sz=A;
    vector<int> arr[A];
    bool vis[A];
    memset(vis,0,sizeof(vis));
    for(auto i:B){
        i[0]--;
        i[1]--;
        arr[i[0]].push_back(i[1]);
        arr[i[1]].push_back(i[0]);
    }
    dfs(arr,vis,0);
    // cout<<"R:"<<res<<" ";
    return res;
}

#2

Can you please explain your approach?