I was wanting to display all of the nodes at a certain level in the tree:
Called by: allNodesAtACertainLevel(0, *whatever level you want*, root);
This produces the correct answer.
private void allNodesAtACertainLevel(int count, int level, Node n){
count += 1;
if(count <= level){
if(n.left != null) allNodesAtACertainLevel(count, level, n.left);
if(n.right != null) allNodesAtACertainLevel(count, level, n.right);
}
else{
System.out.print(n.value);
}
}
This doesn't.
private void allNodesAtACertainLevel(int count, int level, Node n){
if(count < level){
if(n.left != null) allNodesAtACertainLevel(count++, level, n.left);
if(n.right != null) allNodesAtACertainLevel(count++, level, n.right);
}
else{
System.out.print(n.value);
}
}
Could someone explain why?