Preorder traversal of a binary search tree, without using recursion
void preorderTraversal( Node root )
{
NodeStack stack = new NodeStack();
stack.push( root );
while( true )
{
Node curr = stack.pop();
if( curr == null ) break;
curr.printValue();
Node n = curr.getRight();
if( n != null ) stack.push( n );
n = curr.getLeft();
if( n != null ) stack.push( n );
}
}
void preorderTraversal( Node root )
{
NodeStack stack = new NodeStack();
stack.push( root );
while( true )
{
Node curr = stack.pop();
if( curr == null ) break;
curr.printValue();
Node n = curr.getRight();
if( n != null ) stack.push( n );
n = curr.getLeft();
if( n != null ) stack.push( n );
}
}
try post order w.o recursion......thats the tuffest
ReplyDelete