using
System;
using
System.Collections.Generic;
public
class
TreeNode
{
public
int
val;
public
TreeNode left, right;
public
TreeNode(
int
key)
{
val = key;
left =
null
;
right =
null
;
}
}
public
class
GFG{
static
void
fillMap(TreeNode root,
int
left,
Dictionary<
int
,HashSet<
int
>> diag)
{
if
(root ==
null
)
return
;
if
(!diag.ContainsKey(left))
{
diag.Add(left,
new
HashSet<
int
>());
diag[left].Add(root.val);
}
else
diag[left].Add(root.val);
fillMap(root.left, left + 1, diag);
fillMap(root.right, left, diag);
}
static
int
sameDiag(TreeNode root)
{
Dictionary<
int
,HashSet<
int
>> diag =
new
Dictionary<
int
,HashSet<
int
>>();
int
left = 0;
fillMap(root, left, diag);
int
count = 0;
foreach
(KeyValuePair<
int
,HashSet<
int
>> d
in
diag)
{
if
(d.Value.Count == 1)
count += 1;
}
return
count;
}
static
public
void
Main (){
TreeNode root =
new
TreeNode(5);
root.left =
new
TreeNode(6);
root.right =
new
TreeNode(5);
root.left.right =
new
TreeNode(6);
root.right.right =
new
TreeNode(5);
Console.WriteLine(sameDiag(root));
}
}