using
System;
using
System.Collections.Generic;
class
GFG{
public
class
Node
{
public
int
key;
public
Node left, right;
};
static
int
ans;
static
Node newNode(
int
key)
{
Node temp =
new
Node();
temp.key = key;
temp.left = temp.right =
null
;
return
temp;
}
static
void
findkDistinctNodePaths(Node root,
Dictionary<
int
,
int
> freq,
int
distinct_nodes,
int
k)
{
if
(root ==
null
)
return
;
if
(!freq.ContainsKey(root.key))
distinct_nodes++;
if
(distinct_nodes > k)
return
;
if
(freq.ContainsKey(root.key))
{
freq[root.key] = freq[root.key] + 1;
}
else
{
freq.Add(root.key, 1);
}
findkDistinctNodePaths(root.left, freq,
distinct_nodes, k);
findkDistinctNodePaths(root.right, freq,
distinct_nodes, k);
if
(root.left ==
null
&&
root.right ==
null
)
{
if
(distinct_nodes == k)
ans++;
}
}
static
void
printkDistinctNodePaths(Node root,
int
k)
{
Dictionary<
int
,
int
> freq =
new
Dictionary<
int
,
int
>();
int
distinct_nodes = 0;
ans = 0;
findkDistinctNodePaths(root, freq,
distinct_nodes, k);
Console.Write(ans);
}
public
static
void
Main(String[] args)
{
Node root = newNode(2);
root.left = newNode(1);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(2);
root.right.left = newNode(-5);
root.right.right = newNode(3);
int
K = 2;
printkDistinctNodePaths(root, K);
}
}