Engaged Employer
Breadth first search
Anonymous
This assumes the tree is a Binary tree, you can modify it easily to work with a tree with N-children You could implement the tree and queue structures for extra points if you have time. public Node BreadthFirstSearch(int v) { Queue queue = new Queue(); visited = 0; //start with the tree's head queue.Enqueue(Head); while (queue.Count > 0) { Node n = queue.Dequeue(); if (n.Value == v) { return n; } // get both children to the head if (n.Left != null) queue.Enqueue(n.Left); if (n.Right != null) queue.Enqueue(n.Right); } //not found return null; }
Check out your Company Bowl for anonymous work chats.