using
System;
using
System.Collections.Generic;
class
Node {
public
int
val;
public
Node left;
public
Node right;
public
Node(
int
data)
{
val = data;
left = right =
null
;
}
}
class
Program {
static
void
Main(
string
[] args)
{
Node root =
new
Node(5);
root.left =
new
Node(3);
root.right =
new
Node(2);
root.left.left =
new
Node(1);
root.left.right =
new
Node(4);
root.right.left =
new
Node(1);
root.right.right =
new
Node(2);
compressTree(root);
}
public
static
void
evalComp(List<
int
> arr)
{
int
ans = 0;
int
getBit = 1;
for
(
int
i = 0; i < 32; i++) {
int
S = 0;
int
NS = 0;
foreach
(
int
j
in
arr)
{
if
((getBit & j) != 0)
S++;
else
NS++;
}
if
(S > NS)
ans += (
int
)Math.Pow(2, i);
getBit <<= 1;
}
Console.Write(ans +
" "
);
}
public
static
void
Trav(Node root,
int
hd,
Dictionary<
int
, List<
int
> > mp)
{
if
(root ==
null
)
return
;
if
(mp.ContainsKey(hd) ==
false
)
mp[hd] =
new
List<
int
>();
mp[hd].Add(root.val);
Trav(root.left, hd - 1, mp);
Trav(root.right, hd + 1, mp);
}
public
static
void
compressTree(Node root)
{
Dictionary<
int
, List<
int
> > mp
=
new
Dictionary<
int
, List<
int
> >();
Trav(root, 0, mp);
int
lower = Int32.MaxValue, upper = Int32.MinValue;
foreach
(KeyValuePair<
int
, List<
int
> > i
in
mp)
{
lower = Math.Min(lower, i.Key);
upper = Math.Max(upper, i.Key);
}
for
(
int
i = lower; i <= upper; i++)
evalComp(mp[i]);
}
}