import
java.util.HashMap;
import
java.util.Map;
class
GFG{
static
class
Node
{
int
data;
Node left, right;
};
static
Node newnode(
int
data)
{
Node node =
new
Node();
node.data = data;
node.left = node.right =
null
;
return
(node);
}
static
void
pathCountUtil(Node node,
HashMap<Integer, Integer> m,
int
path_len)
{
if
(node ==
null
)
return
;
if
(node.left ==
null
&& node.right ==
null
)
{
if
(!m.containsKey(path_len))
m.put(path_len,
0
);
m.put(path_len, m.get(path_len) +
1
);
return
;
}
pathCountUtil(node.left, m, path_len +
1
);
pathCountUtil(node.right, m, path_len +
1
);
}
static
void
pathCounts(Node root)
{
HashMap<Integer, Integer> m =
new
HashMap<>();
pathCountUtil(root, m,
1
);
for
(Map.Entry<Integer,
Integer> entry : m.entrySet())
{
System.out.printf(
"%d paths have length %d\n"
,
entry.getValue(),
entry.getKey());
}
}
public
static
void
main(String[] args)
{
Node root = newnode(
8
);
root.left = newnode(
5
);
root.right = newnode(
4
);
root.left.left = newnode(
9
);
root.left.right = newnode(
7
);
root.right.right = newnode(
11
);
root.right.right.left = newnode(
3
);
pathCounts(root);
}
}