Заменить значения елементов дерева, которие состоят из одинаковых цифр
Как заменить нулями повторяющиеся элементы в бинарном дереве?
`public class Leaf
{
public int Data;
public Leaf Left;
public Leaf Right;
public Leaf(int data)
{
Data = data;
Left = Right = null;
}
public void AddNode(int data){
if (data <= Data) {
if (Left == null)
Left = new Leaf(data);
else
Left.AddNode(data);
}
else if (data >= Data) {
if (Right == null)
Right = new Leaf(data);
else
Right.AddNode(data);
}
}
class BinaryTree
{
public static void PostPrintTree(Leaf root)
{
if (root != null)
{
PostPrintTree(root.Left);
Console.Write($"{root.Data} ");
PostPrintTree(root.Right);
}
}
public static void ChangeTree(Leaf root)
{
if (root != null)
{
ChangeTree(root.Left);
if (root.Data < 0)
root.Data = 0;
ChangeTree(root.Right);
}
}
static void Main()
{
Console.Write(" Input the size of the tree: ");
int rootData = int.Parse(Console.ReadLine());
Leaf tree = new Leaf(rootData);
for (int i = 0; i < rootData; i++)
{
Console.Write($" Element {i + 1} = ");
int value = int.Parse(Console.ReadLine());
tree.AddNode(value);
}
Console.WriteLine("\n Output of tree elements");
PostPrintTree(tree);
Console.WriteLine();
ChangeTree(tree);
Console.WriteLine();
PostPrintTree(tree);
Console.ReadLine();
}
}
Например, если элементы будут такие: 5 3 4 3 7 6 3
На выходе должно получиться так: 5 0 4 0 7 6 0