using
System;
using
System.Collections.Generic;
class
GFG {
class
Node
{
public
int
data;
public
Node left;
public
Node right;
public
Node(
int
item)
{
data = item;
left = right =
null
;
}
};
static
int
maxx = 0;
static
Dictionary<
int
,
int
> hm_min =
new
Dictionary<
int
,
int
>();
static
Dictionary<
int
,
int
> hm_max =
new
Dictionary<
int
,
int
>();
static
void
getMaxWidthHelper(Node node,
int
lvl,
int
i)
{
if
(node ==
null
) {
return
;
}
if
(hm_max.ContainsKey(lvl)) {
hm_max[lvl] = Math.Max(i, hm_max[lvl]);
}
else
{
hm_max[lvl] = i;
}
if
(hm_min.ContainsKey(lvl)) {
hm_min[lvl] = Math.Min(i, hm_min[lvl]);
}
else
{
hm_min[lvl] = i;
}
getMaxWidthHelper(node.left, lvl + 1,
2 * i + 1);
getMaxWidthHelper(node.right, lvl + 1,
2 * i + 2);
}
static
int
getMaxWidth(Node root)
{
getMaxWidthHelper(root, 0, 0);
foreach
(KeyValuePair<
int
,
int
> lvl
in
hm_max) {
maxx = Math.Max(maxx, hm_max[lvl.Key] - hm_min[lvl.Key] + 1);
}
return
maxx;
}
static
void
Main()
{
Node root =
new
Node(1);
root.left =
new
Node(2);
root.right =
new
Node(3);
root.left.left =
new
Node(4);
root.left.right =
new
Node(5);
root.right.right =
new
Node(8);
root.right.right.left =
new
Node(6);
root.right.right.right =
new
Node(7);
Console.Write(getMaxWidth(root));
}
}