Tuesday, 25 August 2015

Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
    1
   / \
  2   2
 / \ / \
3  4 4  3
But the following is not:

    1
   / \
  2   2
   \   \
   3    3 
 
 
/////////////////// 

#include<iostream>
#include<vector>
using namespace std;
vector<int> pq;
vector<int> pw;
struct TreeNode {
    int val;
    TreeNode *right;
    TreeNode *left;
    TreeNode(int d) : val(d),right(NULL),left(NULL) {}
};
struct TreeNode *newnode(int d)
{
    struct TreeNode *node=new TreeNode(d);
    return node;
};


void in1(struct TreeNode *root)
                   {
                  if ( root) {
                      in1(root->left);
                      pq.push_back(root->val);
                      in1(root->right);
                             }
                   }
       void in2(struct TreeNode *root)
                   {
                  if ( root) {
                        in2(root->right);
                  pw.push_back(root->val);
                      in2(root->left);

                             }
                   }
void inorder(struct TreeNode *root)
{
    int count=0;
   if ( root) {
       struct TreeNode *rt=root->right;
       struct TreeNode *lt=root->left;
       in1(lt);
       in2(rt);
    for(int i=0;i<pq.size();i++)
    {
        if(pq[i] != pw[i])
           {
            count=1;
            break;
           }
    }
   }
    if(count == 1)
        cout<<"No";
    else cout<<"Yes";
}

int main()
{
    struct TreeNode *root;
    root=newnode(1);
    root->left=newnode(2);
    root->left->right=newnode(4);
    root->left->left=newnode(3);
    root->right=newnode(2);
    root->right->right=newnode(4);
    root->right->left=newnode(4);
    inorder(root);
    return 0;
}

Saturday, 25 July 2015

Even Tree in c++ Hackerrank solution


You are given a tree (a simple connected graph with no cycles). You have to remove as many edges from the tree as possible to obtain a forest with the condition that : Each connected component of the forest  should contain an even number of vertices.
To accomplish this, you will remove some edges from the tree. Find out the number of removed edges.
Input Format
The first line of input contains two integers N and M. N is the number of vertices and M is the number of edges.
The next M lines contain two integers ui and vi which specifies an edge of the tree. (1-based index)
Output Format
Print the answer, a single integer.
Constraints
2 <= N <= 100.
Note: The tree in the input will be such that it can always be decomposed into components containing even number of nodes.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int V,E;
    cin>>V>>E;
    int array[E][2];
    for(int i=0;i<E;i++)
    {
         cin>>array[i][0]>>array[i][1];
    }
    int count[V+1];
    for(int j=0;j<=V;j++)
    {
        count[j]=1;
    }
    for(int k=0;k<E;k++)
    {
        count[array[k][1]]=0;
    }

  int c=0,l;
    for(int i=V;i>=0;i--)
    {
        if(count[i]==0)
        {
            for(int k=0;k<E;k++)
            {
                if(array[k][1]== i)
                {   l=array[k][0];
                    if(count[l]%2 != 0)
                    {
                    c=c+count[l];
                    }
                 }
            }
            count[i]=c+1;
        }
        c=0;
    }
    int t=0;
    for(int i=0;i<=V;i++)
        {
            if(count[i]%2 == 0)
            {
                t=t+1;
            }
        }

        cout<<(t-1)<<endl;
    return 0;
}