boolean dfs(TreeNode root, int target) {         if (root == null)             return false;         if (root.data == target)             return true;         return dfs(root.left, target) || dfs(root.right, target);     }


dfs(node start) {
stack s;
s.push(start);
while (s.empty() == false) {
top = s.top();
s.pop();
mark top as visited;

check for termination condition

add all of top's unvisited neighbors to the stack.
mark top as not visited;
}
}

Advertisements