Node node = root;
while(node != null)
{
while(node != null)
{
if(node.right != null)
stack.push(node.right);
stack.push (node);
node = node.left
}
node = stack.pop();
while(!stack.empty() && node.right == null)
{
output node.val;
node = stack.pop();
}
output node.val;
if(stack.empty ())
break;
else
node = stack.pop();
}
I am not sure, if we can do it with stacks, of course I can think of 2 queues approach :-) I am curious if there is any approach that restructures the tree temporarily and does this without a stack.
On 8/13/06, martin-g <[EMAIL PROTECTED]> wrote:
Hi.
Would you help me in composing a nonrecursive algorithm to print out
the keys of BST nodes in ascending order. Any approach will do whether
it uses stack or not.
Thanks in advance
Martin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---
- [algogeeks] Nonrecursive algorithms for printi... martin-g
- [algogeeks] Re: Nonrecursive algorithms f... [EMAIL PROTECTED]
- [algogeeks] Re: Nonrecursive algorithms f... Nat (Padmanabhan Natarajan)
