-
Notifications
You must be signed in to change notification settings - Fork 0
/
SumAndProductOfNodesAtSameLevel.cpp.txt
113 lines (94 loc) · 2.37 KB
/
SumAndProductOfNodesAtSameLevel.cpp.txt
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* Iterative C++ program to find sum of data of all leaves
of a binary tree on same level and then multiply sums
obtained of all levels. */
#include <bits/stdc++.h>
using namespace std;
// A Binary Tree Node
struct Node
{
int data;
struct Node *left, *right;
};
// helper function to check if a Node is leaf of tree
bool isLeaf(Node *root)
{
return (!root->left && !root->right);
}
/* Calculate sum of all leaf Nodes at each level and returns
multiplication of sums */
int sumAndMultiplyLevelData(Node *root)
{
// Tree is empty
if (!root)
return 0;
int mul = 1; /* To store result */
// Create an empty queue for level order tarversal
queue<Node *> q;
// Enqueue Root and initialize height
q.push(root);
// Do level order traversal of tree
while (1)
{
// NodeCount (queue size) indicates number of Nodes
// at current lelvel.
int NodeCount = q.size();
// If there are no Nodes at current level, we are done
if (NodeCount == 0)
break;
// Initialize leaf sum for current level
int levelSum = 0;
// A boolean variable to indicate if found a leaf
// Node at current level or not
bool leafFound = false;
// Dequeue all Nodes of current level and Enqueue all
// Nodes of next level
while (NodeCount > 0)
{
// Process next Node of current level
Node *Node = q.front();
/* if Node is a leaf, update sum at the level */
if (isLeaf(Node))
{
leafFound = true;
levelSum += Node->data;
}
q.pop();
// Add children of Node
if (Node->left != NULL)
q.push(Node->left);
if (Node->right != NULL)
q.push(Node->right);
NodeCount--;
}
// If we found at least one leaf, we multiply
// result with level sum.
if (leafFound)
mul *= levelSum;
}
return mul; // Return result
}
// Utility function to create a new tree Node
Node* newNode(int data)
{
Node *temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
// Driver program to test above functions
int main()
{
Node *root = newNode(2);
root->left = newNode(7);
root->right = newNode(5);
root->left->right = newNode(6);
root->left->left = newNode(8);
root->left->right->left = newNode(1);
root->left->right->right = newNode(11);
root->right->right = newNode(9);
root->right->right->left = newNode(4);
root->right->right->right = newNode(10);
cout << "Final product value = "
<< sumAndMultiplyLevelData(root) << endl;
return 0;
}