Given a binary tree, find the number of subtrees having odd count of even numbers.
Examples:
Input : 2
/ \
1 3
/ \ / \
4 10 8 5
/
6
Output : 6
The subtrees are 4, 6, 1, 8, 3
/ \ / \
4 10 8 5
/
6
2
/ \
1 3
/ \ / \
4 10 8 5
/
6
The idea is to recursively traverse the tree. For every node, recursively count even numbers in left and right subtrees. If root is also even, then add it also to count. If count becomes odd, increment result.
count = 0 // Initialize result
int countSubtrees(root)
{
if (root == NULL)
return 0;
// count even numbers in left subtree
int c = countSubtrees(root-> left);
// Add count of even numbers in right subtree
c += countSubtrees(root-> right);
// check if root data is an even number
if (root-> data % 2 == 0)
c += 1;
// If total count of even numbers
// for the subtree is odd
if (c % 2 != 0)
count++;
// return total count of even
// numbers of the subtree
return c;
}
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* left, *right;
};
struct Node* newNode( int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
int countRec( struct Node* root, int *pcount)
{
if (root == NULL)
return 0;
int c = countRec(root->left, pcount);
c += countRec(root->right, pcount);
if (root->data % 2 == 0)
c += 1;
if (c % 2 != 0)
(*pcount)++;
return c;
}
int countSubtrees(Node *root)
{
int count = 0;
int *pcount = &count;
countRec(root, pcount);
return count;
}
int main()
{
struct Node *root = newNode(2);
root->left = newNode(1);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(10);
root->right->left = newNode(8);
root->right->right = newNode(5);
root->left->right->left = newNode(6);
cout<< "Count = " << countSubtrees(root);
return 0;
}
|
C
#include <bits/stdc++.h>
struct Node
{
int data;
struct Node* left, *right;
};
struct Node* newNode( int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
int countRec( struct Node* root, int *pcount)
{
if (root == NULL)
return 0;
int c = countRec(root->left, pcount);
c += countRec(root->right, pcount);
if (root->data % 2 == 0)
c += 1;
if (c % 2 != 0)
(*pcount)++;
return c;
}
int countSubtrees(Node *root)
{
int count = 0;
int *pcount = &count;
countRec(root, pcount);
return count;
}
int main()
{
struct Node *root = newNode(2);
root->left = newNode(1);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(10);
root->right->left = newNode(8);
root->right->right = newNode(5);
root->left->right->left = newNode(6);
printf ( "Count = %d" , countSubtrees(root));
return 0;
}
|
Java
import java.util.*;
class GfG {
static class Node
{
int data;
Node left, right;
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
static class P
{
int pcount = 0 ;
}
static int countRec(Node root, P p)
{
if (root == null )
return 0 ;
int c = countRec(root.left, p);
c += countRec(root.right, p);
if (root.data % 2 == 0 )
c += 1 ;
if (c % 2 != 0 )
(p.pcount)++;
return c;
}
static int countSubtrees(Node root)
{
P p = new P();
countRec(root, p);
return p.pcount;
}
public static void main(String[] args)
{
Node root = newNode( 2 );
root.left = newNode( 1 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.left.right = newNode( 10 );
root.right.left = newNode( 8 );
root.right.right = newNode( 5 );
root.left.right.left = newNode( 6 );
System.out.println( "Count = " + countSubtrees(root));
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = self .right = None
def countRec(root, pcount):
if (root = = None ):
return 0
c = countRec(root.left, pcount)
c + = countRec(root.right, pcount)
if (root.data % 2 = = 0 ):
c + = 1
if c % 2 ! = 0 :
pcount[ 0 ] + = 1
return c
def countSubtrees(root):
count = [ 0 ]
pcount = count
countRec(root, pcount)
return count[ 0 ]
if __name__ = = '__main__' :
root = newNode( 2 )
root.left = newNode( 1 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 10 )
root.right.left = newNode( 8 )
root.right.right = newNode( 5 )
root.left.right.left = newNode( 6 )
print ( "Count = " , countSubtrees(root))
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left, right;
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
public class P
{
public int pcount = 0;
}
static int countRec(Node root, P p)
{
if (root == null )
return 0;
int c = countRec(root.left, p);
c += countRec(root.right, p);
if (root.data % 2 == 0)
c += 1;
if (c % 2 != 0)
(p.pcount)++;
return c;
}
static int countSubtrees(Node root)
{
P p = new P();
countRec(root, p);
return p.pcount;
}
public static void Main(String[] args)
{
Node root = newNode(2);
root.left = newNode(1);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(10);
root.right.left = newNode(8);
root.right.right = newNode(5);
root.left.right.left = newNode(6);
Console.WriteLine( "Count = " +
countSubtrees(root));
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data = data;
this .left = this .right = null ;
}
}
class P
{
constructor()
{
this .pcount = 0;
}
}
function countRec(root, p)
{
if (root == null )
return 0;
let c = countRec(root.left, p);
c += countRec(root.right, p);
if (root.data % 2 == 0)
c += 1;
if (c % 2 != 0)
(p.pcount)++;
return c;
}
function countSubtrees(root)
{
let p = new P();
countRec(root, p);
return p.pcount;
}
let root = new Node(2);
root.left = new Node(1);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(10);
root.right.left = new Node(8);
root.right.right = new Node(5);
root.left.right.left = new Node(6);
document.write( "Count = " + countSubtrees(root) + "<br>" );
</script>
|
Time complexity: O(n) // where n is the number of nodes in the binary tree.
Space complexity: O(n)
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.