1
public void revTraversal(BinaryTree binaryTree)
{
    Queue<Node> queue = new LinkedList<Node>();
    Stack<Node> stack = new Stack<Node>();
    Node temp;
    queue.add(root);
    while(!queue.isEmpty()){
        temp = queue.poll();
        if(temp.rightNode!=null)
            queue.add(temp.rightNode);
        if(temp.lefNode!=null)
            queue.add(temp.lefNode);
        stack.push(temp);
    }
    while(!stack.isEmpty())System.out.println("Nodes are"+ stack.pop().tData);
}

Why can't we simply iterate the Binary with right child before left child and pushh all those values in stack directly. Why we using a queue as backing datastructure ?

Akhil Gupta
  • 265
  • 1
  • 14
  • 1
    In this case, you could skip the stack and print it out directly. Did you try to check this yourself? – Ami Tavory Jun 02 '15 at 09:50
  • Though I was able to traverse the Tree in reverse order partially by level order traversal from right child to left child and pushing those nodes into the stack and pop() the stack to get the desired order but din't understood why many people are suggesting to use queue at first place and than pushing and popping from stack – Akhil Gupta Jun 02 '15 at 10:35

0 Answers0