forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kopp.cpp
47 lines (39 loc) · 875 Bytes
/
kopp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Recursive C++ program to print odd level nodes
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left, *right;
};
void printOddNodes(Node *root, bool isOdd = true)
{
// If empty tree
if (root == NULL)
return;
// If current node is of odd level
if (isOdd)
cout << root->data << " " ;
// Recur for children with isOdd
// switched.
printOddNodes(root->left, !isOdd);
printOddNodes(root->right, !isOdd);
}
// Utility method to create a node
struct Node* newNode(int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
// Driver code
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;
}