xxxxxxxxxx
var i = 0, len = myArray.length;
while (i < len) {
// your code
i++
}
xxxxxxxxxx
<> Preorder Root -> Left -> Right
<> Inorder Left -> Root -> Right
<> Postorder Left -> Right -> Root
xxxxxxxxxx
Following is a simple stack based iterative process to print Preorder traversal.
Create an empty stack nodeStack and push root node to stack.
Do the following while nodeStack is not empty.
Pop an item from the stack and print it.
Push right child of a popped item to stack
Push left child of a popped item to stack
The right child is pushed before the left child to make sure that the left subtree is processed first.