import
java.util.HashMap;
import
java.util.LinkedList;
import
java.util.Queue;
class
GFG{
static
class
Node
{
int
data;
Node left, right;
public
Node(
int
data)
{
this
.data = data;
this
.left =
this
.right =
null
;
}
}
static
class
Pair
{
Node node;
int
data;
public
Pair(Node node,
int
data)
{
this
.node = node;
this
.data = data;
}
}
static
int
SumAtMaxLevel(Node root)
{
HashMap<Integer,
Integer> mp =
new
HashMap<>();
Queue<Pair> q =
new
LinkedList<>();
q.add(
new
Pair(root,
0
));
while
(!q.isEmpty())
{
Pair temp = q.poll();
int
depth = temp.data;
if
(!mp.containsKey(depth))
mp.put(depth,
0
);
mp.put(depth, mp.get(depth) +
temp.node.data);
if
(temp.node.left !=
null
)
q.add(
new
Pair(temp.node.left,
depth +
1
));
if
(temp.node.right !=
null
)
q.add(
new
Pair(temp.node.right,
depth +
1
));
}
Object[] values = mp.values().toArray();
return
(
int
) values[values.length -
1
];
}
public
static
void
main(String[] args)
{
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.left =
new
Node(
6
);
root.right.right =
new
Node(
7
);
System.out.println(SumAtMaxLevel(root));
}
}