using
System;
using
System.Collections;
using
System.Collections.Generic;
class
Node {
public
int
data;
public
Node left, right;
public
Node(
int
item)
{
data = item;
left = right =
null
;
}
}
public
class
GFG {
Node root;
public
GFG()
{
root =
null
;
}
static
Node newNode(
int
data)
{
Node temp =
new
Node(data);
return
(temp);
}
static
Dictionary<
int
,
int
> level_sum =
new
Dictionary<
int
,
int
>();
static
void
productOfLevelSumUtil(Node root,
int
level)
{
if
(root ==
null
)
return
;
if
(root.left ==
null
&& root.right ==
null
)
{
if
(level_sum.ContainsKey(level))
{
level_sum[level] += root.data;
}
else
{
level_sum[level] = root.data;
}
}
productOfLevelSumUtil(root.left, level + 1);
productOfLevelSumUtil(root.right, level + 1);
}
static
int
productOfLevelSum(Node root)
{
productOfLevelSumUtil(root, 0);
int
prod = 1;
foreach
(KeyValuePair<
int
,
int
> it
in
level_sum)
{
prod *= it.Value;
}
return
prod;
}
static
void
Main() {
GFG tree =
new
GFG();
tree.root = newNode(2);
tree.root.left = newNode(7);
tree.root.right = newNode(5);
tree.root.left.right = newNode(6);
tree.root.left.left = newNode(8);
tree.root.left.right.left = newNode(1);
tree.root.left.right.right = newNode(11);
tree.root.right.right = newNode(9);
tree.root.right.right.left = newNode(4);
tree.root.right.right.right = newNode(10);
Console.Write(
"Final product is = "
+ productOfLevelSum(tree.root));
}
}