Given a Binary Tree, the task is to check if the binary tree is an Even-Odd binary tree or not.
A Binary Tree is called an Even-Odd Tree when all the nodes which are at even levels have even values (assuming root to be at level 0) and all the nodes which are at odd levels have odd values.
Examples:
Input:
2
/ \
3 9
/ \ \
4 10 6
Output: YES
Explanation:
Only node on level 0 (even) is 2 (even).
Nodes present in level 1 are 3 and 9 (both odd).
Nodes present in level 2 are 4, 10 and 6 (all even).
Therefore, the Binary tree is an odd-even binary tree.
Input:
4
/ \
3 7
/ \ \
4 10 5
Output: NO
Approach: Follow the steps below to solve the problem:
- The idea is to perform level-order traversal and check if the nodes present on even levels are even valued or not and nodes present on odd levels are odd valued or not.
- If any node at an odd level is found to have odd value or vice-versa, then print “NO“.
- Otherwise, after complete traversal of the tree, print “YES“.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int val;
Node *left, *right;
};
struct Node* newNode( int data)
{
struct Node* temp = ( struct Node*) malloc ( sizeof ( struct Node));
temp->val = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
bool isEvenOddBinaryTree(Node *root)
{
if (root == NULL)
return true ;
queue<Node*> q;
q.push(root);
int level = 0;
while (!q.empty())
{
int size = q.size();
for ( int i = 0; i < size; i++)
{
Node *node = q.front();
if (level % 2 == 0)
{
if (node->val % 2 == 1)
return false ;
}
else if (level % 2 == 1)
{
if (node->val % 2 == 0)
return true ;
}
if (node->left != NULL)
{
q.push(node->left);
}
if (node->right != NULL)
{
q.push(node->right);
}
}
level++;
}
return true ;
}
int main()
{
Node *root = NULL;
root = newNode(2);
root->left = newNode(3);
root->right = newNode(9);
root->left->left = newNode(4);
root->left->right = newNode(10);
root->right->right = newNode(6);
if (isEvenOddBinaryTree(root))
cout << "YES" ;
else
cout << "NO" ;
}
|
Java
import java.util.*;
class GfG {
static class Node {
int val;
Node left, right;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.val = data;
temp.left = null ;
temp.right = null ;
return temp;
}
public static boolean
isEvenOddBinaryTree(Node root)
{
if (root == null )
return true ;
Queue<Node> q
= new LinkedList<>();
q.add(root);
int level = 0 ;
while (!q.isEmpty()) {
int size = q.size();
for ( int i = 0 ; i < size; i++) {
Node node = q.poll();
if (level % 2 == 0 ) {
if (node.val % 2 == 1 )
return false ;
}
else if (level % 2 == 1 ) {
if (node.val % 2 == 0 )
return false ;
}
if (node.left != null ) {
q.add(node.left);
}
if (node.right != null ) {
q.add(node.right);
}
}
level++;
}
return true ;
}
public static void main(String[] args)
{
Node root = null ;
root = newNode( 2 );
root.left = newNode( 3 );
root.right = newNode( 9 );
root.left.left = newNode( 4 );
root.left.right = newNode( 10 );
root.right.right = newNode( 6 );
if (isEvenOddBinaryTree(root)) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
}
|
Python3
class Node:
def __init__( self , data):
self .left = None
self .right = None
self .val = data
def newNode(data):
temp = Node(data)
return temp
def isEvenOddBinaryTree(root):
if (root = = None ):
return True
q = []
q.append(root)
level = 0
while ( len (q) ! = 0 ):
size = len (q)
for i in range (size):
node = q[ 0 ]
q.pop( 0 )
if (level % 2 = = 0 ):
if (node.val % 2 = = 1 ):
return False
elif (level % 2 = = 1 ):
if (node.val % 2 = = 0 ):
return False
if (node.left ! = None ):
q.append(node.left)
if (node.right ! = None ):
q.append(node.right)
level + = 1
return True
if __name__ = = "__main__" :
root = None
root = newNode( 2 )
root.left = newNode( 3 )
root.right = newNode( 9 )
root.left.left = newNode( 4 )
root.left.right = newNode( 10 )
root.right.right = newNode( 6 )
if (isEvenOddBinaryTree(root)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
using System.Collections.Generic;
class GfG{
class Node
{
public int val;
public Node left, right;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.val = data;
temp.left = null ;
temp.right = null ;
return temp;
}
static bool isEvenOddBinaryTree(Node root)
{
if (root == null )
return true ;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
int level = 0;
while (q.Count != 0)
{
int size = q.Count;
for ( int i = 0; i < size; i++)
{
Node node = q.Dequeue();
if (level % 2 == 0)
{
if (node.val % 2 == 1)
return false ;
}
else if (level % 2 == 1)
{
if (node.val % 2 == 0)
return false ;
}
if (node.left != null )
{
q.Enqueue(node.left);
}
if (node.right != null )
{
q.Enqueue(node.right);
}
}
level++;
}
return true ;
}
public static void Main(String[] args)
{
Node root = null ;
root = newNode(2);
root.left = newNode(3);
root.right = newNode(9);
root.left.left = newNode(4);
root.left.right = newNode(10);
root.right.right = newNode(6);
if (isEvenOddBinaryTree(root))
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .left = null ;
this .right = null ;
this .val = data;
}
}
function newNode(data)
{
let temp = new Node(data);
return temp;
}
function isEvenOddBinaryTree(root)
{
if (root == null )
return true ;
let q = [];
q.push(root);
let level = 0;
while (q.length > 0)
{
let size = q.length;
for (let i = 0; i < size; i++)
{
let node = q[0];
q.shift();
if (level % 2 == 0)
{
if (node.val % 2 == 1)
return false ;
}
else if (level % 2 == 1)
{
if (node.val % 2 == 0)
return false ;
}
if (node.left != null )
{
q.push(node.left);
}
if (node.right != null )
{
q.push(node.right);
}
}
level++;
}
return true ;
}
let root = null ;
root = newNode(2);
root.left = newNode(3);
root.right = newNode(9);
root.left.left = newNode(4);
root.left.right = newNode(10);
root.right.right = newNode(6);
if (isEvenOddBinaryTree(root))
{
document.write( "YES" );
}
else
{
document.write( "NO" );
}
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Method 2 (Using parent child difference)
Approach:
The idea is to check for the absolute difference between the child and parent node.
1. If the root node is odd, return “NO“.
2. If root node is even, then the child nodes should be odd, so difference should always come as odd. In true case return “YES“, else return “NO“.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int data;
Node *left, *right;
};
Node* newNode( int data)
{
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
bool BSTUtil(Node * root){
if (root==NULL)
return true ;
if (root->left!=NULL && abs (root->data - root->left->data)%2==0)
return false ;
if (root->right!=NULL && abs (root->data - root->right->data)%2==0)
return false ;
return BSTUtil(root->left) && BSTUtil(root->right);
}
bool isEvenOddBinaryTree(Node * root){
if (root==NULL)
return true ;
if (root->data%2 != 0)
return false ;
return BSTUtil(root);
}
int main()
{
Node* root = newNode(5);
root->left = newNode(2);
root->right = newNode(6);
root->left->left = newNode(1);
root->left->right = newNode(5);
root->right->right = newNode(7);
root->left->right->left = newNode(12);
root->right->right->right = newNode(14);
root->right->right->left = newNode(16);
if (BSTUtil(root))
cout<< "YES" ;
else
cout<< "NO" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static class Node
{
public int data;
public Node left, right;
public Node(){
data = 0 ;
left = right = null ;
}
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
static boolean BSTUtil(Node root){
if (root == null )
return true ;
if (root.left != null && Math.abs(root.data - root.left.data) % 2 == 0 )
return false ;
if (root.right != null && Math.abs(root.data - root.right.data) % 2 == 0 )
return false ;
return BSTUtil(root.left) && BSTUtil(root.right);
}
static boolean isEvenOddBinaryTree(Node root){
if (root == null )
return true ;
if (root.data% 2 != 0 )
return false ;
return BSTUtil(root);
}
public static void main(String args[])
{
Node root = newNode( 5 );
root.left = newNode( 2 );
root.right = newNode( 6 );
root.left.left = newNode( 1 );
root.left.right = newNode( 5 );
root.right.right = newNode( 7 );
root.left.right.left = newNode( 12 );
root.right.right.right = newNode( 14 );
root.right.right.left = newNode( 16 );
if (BSTUtil(root))
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0
self .left = self .right = None
def newNode(data):
temp = Node()
temp.data = data
temp.left = temp.right = None
return temp
def BSTUtil(root):
if (root = = None ):
return True
if (root.left ! = None and abs (root.data - root.left.data) % 2 = = 0 ):
return False
if (root.right ! = None and abs (root.data - root.right.data) % 2 = = 0 ):
return False
return BSTUtil(root.left) and BSTUtil(root.right)
def isEvenOddBinaryTree(root):
if (root = = None ):
return True
if (root.data % 2 ! = 0 ):
return False
return BSTUtil(root)
root = newNode( 5 )
root.left = newNode( 2 )
root.right = newNode( 6 )
root.left.left = newNode( 1 )
root.left.right = newNode( 5 )
root.right.right = newNode( 7 )
root.left.right.left = newNode( 12 )
root.right.right.right = newNode( 14 )
root.right.right.left = newNode( 16 )
if (BSTUtil(root)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
class Node
{
public int data;
public Node left, right;
public Node()
{
data = 0;
left = right = null ;
}
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
static bool BSTUtil(Node root)
{
if (root == null )
return true ;
if (root.left != null && Math.Abs(root.data - root.left.data) % 2 == 0)
return false ;
if (root.right != null && Math.Abs(root.data - root.right.data) % 2 == 0)
return false ;
return BSTUtil(root.left) && BSTUtil(root.right);
}
static bool isEvenOddBinaryTree(Node root)
{
if (root == null )
return true ;
if (root.data % 2 != 0)
return false ;
return BSTUtil(root);
}
static void Main( string [] args)
{
Node root = newNode(5);
root.left = newNode(2);
root.right = newNode(6);
root.left.left = newNode(1);
root.left.right = newNode(5);
root.right.right = newNode(7);
root.left.right.left = newNode(12);
root.right.right.right = newNode(14);
root.right.right.left = newNode(16);
if (BSTUtil(root))
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
class Node{
constructor(){
this .data = 0
this .left = this .right = null
}
}
function newNode(data){
let temp = new Node()
temp.data = data
temp.left = temp.right = null
return temp
}
function BSTUtil(root){
if (root == null )
return true
if (root.left != null && Math.abs(root.data - root.left.data) % 2 == 0)
return false
if (root.right != null && Math.abs(root.data - root.right.data) % 2 == 0)
return false
return BSTUtil(root.left) && BSTUtil(root.right)
}
function isEvenOddBinaryTree(root){
if (root == null )
return true
if (root.data%2 != 0)
return false
return BSTUtil(root)
}
let root = newNode(5)
root.left = newNode(2)
root.right = newNode(6)
root.left.left = newNode(1)
root.left.right = newNode(5)
root.right.right = newNode(7)
root.left.right.left = newNode(12)
root.right.right.right = newNode(14)
root.right.right.left = newNode(16)
if (BSTUtil(root))
document.write( "YES" , "</br>" )
else
document.write( "NO" , "</br>" )
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach(Using recursion):
A simple and efficient approach as compared to above approach.
Follow the below steps to solve the given problem:
1). We simply traverse the whole binary tree recursively in inorder fashing and keep track to level at each node.
2). We will check at each level that current node data is follow the even-odd tree constraints. If the node don’t follow the constraint the we store the false in ans and simply return.
3). In driver code we will print our answer based on ans variable.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node *left, *right;
};
Node* newNode( int data){
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
void isEvenOddBinaryTree(Node * root, bool & ans, int level){
if (root == NULL) return ;
isEvenOddBinaryTree(root->left, ans, level+1);
if (level % 2 == 0){
if (root->data % 2 != 0) ans = false ;
}
else {
if (root->data % 2 == 0) ans = false ;
}
isEvenOddBinaryTree(root->right, ans, level+1);
}
int main(){
Node *root = NULL;
root = newNode(2);
root->left = newNode(3);
root->right = newNode(9);
root->left->left = newNode(4);
root->left->right = newNode(10);
root->right->right = newNode(6);
bool ans = true ;
isEvenOddBinaryTree(root, ans, 0);
if (ans)
cout<< "YES" ;
else
cout<< "NO" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Node {
int data;
Node left, right;
public Node( int data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
}
class GFG {
static void isEvenOddBinaryTree(Node root, boolean ans,
int level)
{
if (root == null )
return ;
isEvenOddBinaryTree(root.left, ans, level + 1 );
if (level % 2 == 0 ) {
if (root.data % 2 != 0 )
ans = false ;
}
else {
if (root.data % 2 == 0 )
ans = false ;
}
isEvenOddBinaryTree(root.right, ans, level + 1 );
}
public static void main(String[] args)
{
Node root = null ;
root = new Node( 2 );
root.left = new Node( 3 );
root.right = new Node( 9 );
root.left.left = new Node( 4 );
root.left.right = new Node( 10 );
root.right.right = new Node( 6 );
boolean ans = true ;
isEvenOddBinaryTree(root, ans, 0 );
if (ans)
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def newNode(data):
return Node(data)
ans = True
def isEvenOddBinaryTree(root, level):
global ans
if (root is None ):
return
isEvenOddBinaryTree(root.left, level + 1 )
if (level % 2 = = 0 ):
if (root.data % 2 ! = 0 ):
ans = False
else :
if (root.data % 2 = = 0 ):
ans = False
isEvenOddBinaryTree(root.right, level + 1 )
root = None
root = newNode( 2 )
root.left = newNode( 3 )
root.right = newNode( 9 )
root.left.left = newNode( 4 )
root.left.right = newNode( 10 )
root.right.right = newNode( 6 )
isEvenOddBinaryTree(root, 0 )
if (ans):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections;
class Gfg
{
class Node{
public int data;
public Node left, right;
public Node( int data)
{
this .data=data;
this .left= null ;
this .right= null ;
}
}
static void isEvenOddBinaryTree(Node root, bool ans, int level)
{
if (root == null )
return ;
isEvenOddBinaryTree(root.left, ans, level+1);
if (level % 2 == 0){
if (root.data % 2 != 0)
ans = false ;
}
else {
if (root.data % 2 == 0)
ans = false ;
}
isEvenOddBinaryTree(root.right, ans, level+1);
}
static void Main( string [] args)
{
Node root = null ;
root = new Node(2);
root.left = new Node(3);
root.right = new Node(9);
root.left.left = new Node(4);
root.left.right = new Node(10);
root.right.right = new Node(6);
bool ans = true ;
isEvenOddBinaryTree(root, ans, 0);
if (ans)
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
Javascript
class Node{
constructor(data){
this .data = data;
this .left = null ;
this .right = null ;
}
}
function newNode(data){
return new Node(data);
}
let ans = true ;
function isEvenOddBinaryTree(root, level){
if (root == null ) return ;
isEvenOddBinaryTree(root.left, level+1);
if (level % 2 == 0){
if (root.data % 2 != 0) ans = false ;
}
else {
if (root.data % 2 == 0) ans = false ;
}
isEvenOddBinaryTree(root.right, level+1);
}
let root = null ;
root = newNode(2);
root.left = newNode(3);
root.right = newNode(9);
root.left.left = newNode(4);
root.left.right = newNode(10);
root.right.right = newNode(6);
isEvenOddBinaryTree(root, 0);
if (ans)
console.log( "YES" );
else
console.log( "NO" );
|
Time Complexity: O(N) where N is the number of nodes in given binary tree.
Auxiliary Space: O(h) where h is the height of given binary tree.