using
System;
using
System.Collections;
using
System.Collections.Generic;
class
GFG
{
public
class
Node {
public
int
data;
public
Node left, right;
};
static
Dictionary<
int
, List<
int
>> resultMap =
new
Dictionary<
int
, List<
int
>>();
static
int
fillMap(Node root)
{
if
(root ==
null
)
return
0;
int
LsubTreeDepth = fillMap(root.left);
int
RsubTreeDepth = fillMap(root.right);
int
depth = Math.Max(LsubTreeDepth, RsubTreeDepth) + 1;
if
(!resultMap.ContainsKey(depth)){
resultMap.Add(depth,
new
List<
int
>());
}
resultMap[depth].Add(root.data);
return
depth;
}
static
void
printLeafNodes(Node root)
{
if
(root ==
null
)
return
;
int
maxDepth = fillMap(root);
for
(
int
i = 1; i <= maxDepth; i++) {
List<
int
> tempVector = resultMap[i];
int
vSize = tempVector.Count;
Console.Write(
"Iteration "
+ i +
" : "
);
for
(
int
j = 0 ; j < vSize ; j++)
Console.Write(tempVector[j] +
" "
);
Console.WriteLine(
""
);
}
}
static
Node newNode(
int
data)
{
Node temp =
new
Node();
temp.data = data;
temp.left = temp.right =
null
;
return
temp;
}
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);
printLeafNodes(root);
}
}