-
Notifications
You must be signed in to change notification settings - Fork 691
/
Print.nodes.odd.level.c++
71 lines (61 loc) · 1.6 KB
/
Print.nodes.odd.level.c++
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Iterative C++ program to print odd level nodes
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* left, *right;
};
// Iterative method to do level order traversal line by line
void printOddNodes(Node *root)
{
// Base Case
if (root == NULL) return;
// Create an empty queue for level
// order tarversal
queue<Node *> q;
// Enqueue root and initialize level as odd
q.push(root);
bool isOdd = true;
while (1)
{
// nodeCount (queue size) indicates
// number of nodes at current level.
int nodeCount = q.size();
if (nodeCount == 0)
break;
// Dequeue all nodes of current level
// and Enqueue all nodes of next level
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;
}
}
// 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;
}