Print Level order by level

View previous topic View next topic Go down

Print Level order by level

Post  Admin on Mon Oct 24, 2011 7:50 pm

public void Level(TreeNode root){
Queue<TreeNode> q1=new Queue<TreeNode>();
Queue<TreeNode> q2=new Queue<TreeNode>();
q1.enqueue(root);
while(!q1.isEmpty()||(!q2.isEmpty())){
while(!q1.isEmpty()){
TreeNode temp=q1.dequeue();
System.out.print(temp.value);
if(temp.left)
q2.enqueue(temp.left);
if(temp.right)
q2.enqueue(temp.right);
}
System.out.println("");
while(!q2.isEmpty()){
TreeNode temp1=q2.dequeue();
System.out.print(temp1.value);
if(temp1.left)
q1.enqueue(temp.left);
if(temp2.right)
q2.enqueue(temp.right);
}
System.out.println(" ");

}
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum