using
System;
using
System.Collections.Generic;
class
TreeNode {
public
int
val;
public
TreeNode left, right;
public
TreeNode(
int
v)
{
val = v;
left =
null
;
right =
null
;
}
}
class
GFG {
static
int
FindCompressValue(List<
int
> arr)
{
int
ans = 0;
int
getBit = 1;
for
(
int
i = 0; i < 32; i++) {
int
S = 0;
int
NS = 0;
for
(
int
j = 0; j < arr.Count; j++) {
if
((getBit & arr[j]) != 0) {
S += 1;
}
else
{
NS += 1;
}
}
if
(S > NS) {
ans += (
int
)Math.Pow(2, i);
}
getBit <<= 1;
}
return
ans;
}
static
void
DiagonalOrder(TreeNode root,
int
d,
Dictionary<
int
, List<
int
> > mp)
{
if
(root ==
null
) {
return
;
}
if
(!mp.ContainsKey(d)) {
mp[d] =
new
List<
int
>();
}
mp[d].Add(root.val);
DiagonalOrder(root.left, d + 1, mp);
DiagonalOrder(root.right, d, mp);
}
static
int
FindInteger(TreeNode root)
{
Dictionary<
int
, List<
int
> > mp
=
new
Dictionary<
int
, List<
int
> >();
DiagonalOrder(root, 0, mp);
List<
int
> arr =
new
List<
int
>();
foreach
(KeyValuePair<
int
, List<
int
> > entry
in
mp)
{
arr.Add(FindCompressValue(entry.Value));
}
return
FindCompressValue(arr);
}
static
void
Main(
string
[] args)
{
TreeNode root =
new
TreeNode(6);
root.left =
new
TreeNode(5);
root.right =
new
TreeNode(3);
root.left.left =
new
TreeNode(3);
root.left.right =
new
TreeNode(5);
root.right.left =
new
TreeNode(3);
root.right.right =
new
TreeNode(4);
Console.WriteLine(FindInteger(root));
}
}