Given a binary tree, print nodes of odd level in any order. Root is considered at level 1.
For example consider the following tree
1
/ \
2 3
/ \ \
4 5 6
/ \ /
7 8 9
Output 1 4 5 6
Method 1 (Recursive): The idea is to pass initial level as odd and switch level flag in every recursive call. For every node, if odd flag is set, then print it.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left, *right;
};
void printOddNodes(Node *root, bool isOdd = true )
{
if (root == NULL)
return ;
if (isOdd)
cout << root->data << " " ;
printOddNodes(root->left, !isOdd);
printOddNodes(root->right, !isOdd);
}
struct Node* newNode( int data)
{
struct Node* node = new Node;
node->data = data;
node->left = 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);
printOddNodes(root);
return 0;
}
|
Java
class GfG {
static class Node {
int data;
Node left, right;
}
static void printOddNodes(Node root, boolean isOdd)
{
if (root == null )
return ;
if (isOdd == true )
System.out.print(root.data + " " );
printOddNodes(root.left, !isOdd);
printOddNodes(root.right, !isOdd);
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
printOddNodes(root, true );
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = self .right = None
def printOddNodes(root, isOdd = True ):
if (root = = None ):
return
if (isOdd):
print (root.data, end = " " )
printOddNodes(root.left, not isOdd)
printOddNodes(root.right, not isOdd)
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
printOddNodes(root)
|
C#
using System;
public class GfG
{
public class Node
{
public int data;
public Node left, right;
}
public static void printOddNodes(Node root, bool isOdd)
{
if (root == null )
{
return ;
}
if (isOdd == true )
{
Console.Write(root.data + " " );
}
printOddNodes(root.left, !isOdd);
printOddNodes(root.right, !isOdd);
}
public static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
public static void Main( string [] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
printOddNodes(root, true );
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function printOddNodes(root, isOdd)
{
if (root == null )
return ;
if (isOdd == true )
document.write(root.data + " " );
printOddNodes(root.left, !isOdd);
printOddNodes(root.right, !isOdd);
}
function newNode(data)
{
let node = new Node(data);
return (node);
}
let root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
printOddNodes(root, true );
</script>
|
Time complexity : O(n)
Auxiliary Space: O(n) for Recursive Stack Space in case of Skewed Tree
Method 2 (Iterative): The above code prints nodes in preorder way. If we wish to print nodes level by level, we can use level order traversal. The idea is based on Print level order traversal line by line
We traverse nodes level by level. We switch odd level flag after every level.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left, *right;
};
void printOddNodes(Node *root)
{
if (root == NULL) return ;
queue<Node *> q;
q.push(root);
bool isOdd = true ;
while (1)
{
int nodeCount = q.size();
if (nodeCount == 0)
break ;
while (nodeCount > 0)
{
Node *node = q.front();
if (isOdd)
cout << node->data << " " ;
q.pop();
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
nodeCount--;
}
isOdd = !isOdd;
}
}
struct Node* newNode( int data)
{
struct Node* node = new Node;
node->data = data;
node->left = 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);
printOddNodes(root);
return 0;
}
|
Java
import java.util.*;
class GfG {
static class Node {
int data;
Node left, right;
}
static void printOddNodes(Node root)
{
if (root == null ) return ;
Queue<Node> q = new LinkedList<Node> ();
q.add(root);
boolean isOdd = true ;
while ( true )
{
int nodeCount = q.size();
if (nodeCount == 0 )
break ;
while (nodeCount > 0 )
{
Node node = q.peek();
if (isOdd == true )
System.out.print(node.data + " " );
q.remove();
if (node.left != null )
q.add(node.left);
if (node.right != null )
q.add(node.right);
nodeCount--;
}
isOdd = !isOdd;
}
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
printOddNodes(root);
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = self .right = None
def printOddNodes(root) :
if (root = = None ):
return
q = []
q.append(root)
isOdd = True
while ( 1 ) :
nodeCount = len (q)
if (nodeCount = = 0 ) :
break
while (nodeCount > 0 ):
node = q[ 0 ]
if (isOdd):
print (node.data, end = " " )
q.pop( 0 )
if (node.left ! = None ) :
q.append(node.left)
if (node.right ! = None ) :
q.append(node.right)
nodeCount - = 1
isOdd = not isOdd
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
printOddNodes(root)
|
C#
using System;
using System.Collections.Generic;
public class GfG
{
public class Node
{
public int data;
public Node left, right;
}
static void printOddNodes(Node root)
{
if (root == null ) return ;
Queue<Node> q = new Queue<Node> ();
q.Enqueue(root);
bool isOdd = true ;
while ( true )
{
int nodeCount = q.Count;
if (nodeCount == 0)
break ;
while (nodeCount > 0)
{
Node node = q.Peek();
if (isOdd == true )
Console.Write(node.data + " " );
q.Dequeue();
if (node.left != null )
q.Enqueue(node.left);
if (node.right != null )
q.Enqueue(node.right);
nodeCount--;
}
isOdd = !isOdd;
}
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
public static void Main(String[] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
printOddNodes(root);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .data=data;
this .left= this .right= null ;
}
}
function printOddNodes(root)
{
if (root == null ) return ;
let q = [];
q.push(root);
let isOdd = true ;
while ( true )
{
let nodeCount = q.length;
if (nodeCount == 0)
break ;
while (nodeCount > 0)
{
let node = q[0];
if (isOdd == true )
document.write(node.data + " " );
q.shift();
if (node.left != null )
q.push(node.left);
if (node.right != null )
q.push(node.right);
nodeCount--;
}
isOdd = !isOdd;
}
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
printOddNodes(root);
</script>
|
Time complexity : O(n)
Auxiliary Space: O(n) for Queue Data Structure
This article is contributed by Pranav. 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.