Hi recently i am working on a binary search tree assignment which require me to get the deepest level of tree using level to level tree traversal. I did it by storing the dequee node into the stack so as to revert the order from the last level to the first. However, i am facing a problem on displaying the last level store in the stack. Can someone help? May i know how to do it? Below is the attachment of my code. Thank in advance=D
This is how my tree look like
level 1 123
/ \
level 2 65 126
\ / \
level 3 78 125 234
Hence i need to print 78,125,234 for the deepest node
public void topDownLevelTraversal(){
if(isEmpty())
return;
//a queue to store tree nodes
Queue q = new Queue();
Stack s = new Stack();
//enqueue the first node
q.enqueue(root);
while(!q.isEmpty()){
BTNode cur = (BTNode)(q.dequeue());
s.push(cur);
if(cur!= null){
if(cur.getLeft() != null){
q.enqueue(cur.getLeft());
}
if(cur.getRight() != null){
q.enqueue(cur.getRight());
}
}
}
}
My printing code
public void print() {
ListNode cur = head;
System.out.println("List content is ");
while (cur != null) {
BTNode c = new BTNode();
c = (BTNode) cur.getItem();
System.out.println(c.getItem().getAccountID());
cur = cur.getNext();
}
System.out.println();
}