I have created a binary tree. I have a function which counts the odd values from nodes of tree.
I am always getting the output as if it is just returning the odd count from node->right.
I am loosing the value of node->left count.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
int getLeafCount(struct node* node, int count)
{
int temp = 0;
if(node!=NULL)
{
if((node -> data %2) != 0)
{
temp++;
count = count + temp;
}
getLeafCount(node->left, count);
getLeafCount(node->right, count);
}
return count;
}
struct node* newNode(int data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
int tot_cnt = getLeafCount(root, 0);
cout << "Total Count: " << tot_cnt;
getchar();
return 0;
}
I am always getting output as 1 instead of 3.
Please help me out.
Thanks