Thursday, August 29, 2013

Coding Exercises Day 27d (Problem #24)


Problem:
Implement Depth First Search for a Binary Tree
public <T> static boolean depthFirstSearch(Node<T extends Comparable<T>>  node, T value) {
  if (null == node) return false;

  if (value.compareTo(node.getValue()) == 0) {
    return true;
  }
  return depthFirstSearch(node.getLeft(), value) || 
         depthFirstSearch(node.getRight(), value);
}

No comments:

Post a Comment