public static void Main(string[] args)
var tree = new Neighbors();
public void CreateNeighbors()
public void ShowNeighbors()
private void CreateNeighbors(Node root)
root.Left.Neighbor = root.Right;
var parentNeighbor = root.Neighbor;
while (parentNeighbor != null)
var neighborNode = parentNeighbor.Left ?? parentNeighbor.Right;
if (neighborNode != null)
root.Left.Neighbor = neighborNode;
parentNeighbor = parentNeighbor.Neighbor;
var parentNeighbor = root.Neighbor;
while (parentNeighbor != null)
var neighborNode = parentNeighbor.Left ?? parentNeighbor.Right;
if (neighborNode != null)
root.Right.Neighbor = neighborNode;
parentNeighbor = parentNeighbor.Neighbor;
CreateNeighbors(root.Right);
CreateNeighbors(root.Left);
private void ShowNeighbors(Node root)
while (currentNode != null)
currentNode = node.Left ?? node.Right;
Console.Write(node.Value + " ");