tsarapin
BAN USER// Reversed BFS traversal
using System;
using System.Collections.Generic;
public class Test
{
static Stack<Node> internalStack = new Stack<Node>();
class Node
{
public Node(int value, Node left, Node right)
{
Left = left;
Right = right;
Value = value;
}
public readonly Node Left;
public readonly Node Right;
public readonly int Value;
}
public static void Main()
{
// your code goes here
var root = new Node(1, new Node(2, new Node(4, null, null), new Node(5, null, null)),
new Node(3, new Node(6, null, null), new Node(7, null, null)));
internalStack.Push(root);
Visit(root);
while(internalStack.Count > 0)
{
var item = internalStack.Pop();
Console.Write(item.Value);
}
}
private static void Visit(Node root)
{
if(root == null) return;
if(root.Right != null)
{
internalStack.Push(root.Right);
}
if(root.Left != null)
{
internalStack.Push(root.Left);
}
Visit(root.Right);
Visit(root.Left);
}
}
- tsarapin November 20, 2016