Skip to content

Instantly share code, notes, and snippets.

@gabrielgreen
Created June 12, 2012 01:39
Show Gist options
  • Save gabrielgreen/2913879 to your computer and use it in GitHub Desktop.
Save gabrielgreen/2913879 to your computer and use it in GitHub Desktop.
ladder depth first
void Main()
{
//Store the root node in Container
//While (there are nodes in Container)
// N = Get the "next" node from Container
// Store all the children of N in Container
// Do some work on N
var paths = new List<int[]>();
int N = 10;
var stack = new Stack();
stack.Push(new int[] { 0 });
while(stack.Count > 0)
{
if(stack.Count > max) max = stack.Count;
var next = stack.Pop() as int[];
int total = next.Sum();
if(total < N + 1)
{
stack.Push(next.Concat(new int [] { 1 }).ToArray());
}
if(total < N + 2)
{
stack.Push(next.Concat(new int [] { 2 }).ToArray());
}
if(next.Sum() == N)
{
paths.Add(next.ToArray());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment