using System.Collections.Generic;
public static void Main()
var list = new List<int> { 4, 2, 1, 3, 5, 6 };
for (var i = 0; i < list.Count; ++i)
root = Tree.insert(root, list[i]);
var res = BTSSequences(root);
Console.WriteLine(JsonConvert.SerializeObject(res));
static List<List<int>> BTSSequences(Node root)
List<Node> roots = new List<Node> { root };
return BTSSequences(roots);
static List<List<int>> BTSSequences(List<Node> roots)
var output = new List<List<int>>();
for (var i = 0; i < roots.Count; i++)
var choices = roots.ToList();
choices.AddRange(new[] { root.Left, root.Right }.Where(c => c != null).ToList());
var itemsList = BTSSequences(choices);
foreach (var items in itemsList)
output.Add(new[] { root.Value }.Concat(items).ToList());
output.Add(new[] { root.Value }.ToList());
public static Node insert(Node root, int val)
else if (val < root.Value)
root.Left = insert(root.Left, val);
root.Right = insert(root.Right, val);
public static void traverse(Node root)
if (root == null) return;
Console.WriteLine(root.Value);