Skip to main content

Count complete odd nodes path of binary tree in c

All odd nodes path from root to leaf in binary tree

C program for Count complete odd nodes path of binary tree. Here more solutions.

// Include header file
#include <stdio.h>
#include <stdlib.h>
/* 
  C program for
  Count odd paths in Binary Tree
*/
// Binary Tree node
typedef struct TreeNode
{
	// Define useful field of TreeNode
	int data;
	struct TreeNode * left;
	struct TreeNode * right;
}TreeNode;

TreeNode * getTreeNode(int data)
{
	// Create dynamic memory of TreeNode
	TreeNode * ref = (TreeNode * ) malloc(sizeof(TreeNode));
	if (ref == NULL)
	{
		// Failed to create memory 
		return NULL;
	}
	// Set node value
	ref->data = data;
	ref->left = NULL;
	ref->right = NULL;
	return ref;
}
typedef struct BinaryTree
{
	// Define useful field of BinaryTree
	struct TreeNode * root;
}BinaryTree;

BinaryTree * getBinaryTree()
{
	// Create dynamic memory of BinaryTree
	BinaryTree * ref = 
      (BinaryTree * ) malloc(sizeof(BinaryTree));
	if (ref == NULL)
	{
		// Failed to create memory 
		return NULL;
	}
	//Set initial tree root
	ref->root = NULL;
	return ref;
}
// Count all paths from root to leaf
// which containing all Odd nodes
int countOddNodePath(TreeNode * node)
{
	if (node == NULL || 
        node->data % 2 == 0)
	{
		// When tree node is null or
		// its contain even value
		return 0;
	}
	else
	{
		if (node->left == NULL && 
            node->right == NULL)
		{
			// When get leaf node And
			// path contain all Odd nodes
			return 1;
		}
		return countOddNodePath(node->left) + 
          countOddNodePath(node->right);
	}
}
int main()
{
	// New of binary tree
	BinaryTree * tree = getBinaryTree();
	/*
	  Construct Binary Tree
	  -----------------------
	         5
	        /  \ 
	       /    \
	      3      9
	     / \    / \
	    7   6  1   3
	   / \    / \   
	  10  3  7   4
	*/
	// Add tree node
	tree->root = getTreeNode(5);
	tree->root->left = getTreeNode(3);
	tree->root->right = getTreeNode(9);
	tree->root->right->right = getTreeNode(3);
	tree->root->left->right = getTreeNode(6);
	tree->root->right->left = getTreeNode(1);
	tree->root->left->left = getTreeNode(7);
	tree->root->left->left->left = getTreeNode(10);
	tree->root->left->left->right = getTreeNode(3);
	tree->root->right->left->right = getTreeNode(4);
	tree->root->right->left->left = getTreeNode(7);
	/*
	  Given Binary Tree
	  -----------------------
	         5
	        /  \ 
	       /    \
	      3      9
	     / \    / \
	    7   6  1   3
	   / \    / \   
	  10  3  7   4
	  Here
	  Odd path from root to leaf
	  -----------------------
	         5
	        /  \ 
	       /    \
	      3      9
	     /      / \
	    7      1   3
	     \    /     
	      3  7    
	  ---------------------
		5->3->7->3    
		5->9->1->7  
	    5->9->3  
	  ----------------------
	  Result : 3 [number of path]
	*/
	// Count odd nodes paths from root
	// to leaf in a binary tree.
	printf("%d\n",countOddNodePath(tree->root));
}

Output

3




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment