140 likes | 227 Views
Preorder Traversal with a Stack. Push the root onto the stack. While the stack is not empty pop the stack and visit it push its two children. 14. 14 84 13 53 06 99 72 43 33 64 97 51 25. Stack. 14. 43. 84. 16. 33. 97. 13. 72. 53. 99. 64. Preorder Traversal with a Stack.
E N D
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 14 14 84 13 53 06 99 72 43 33 64 97 51 25 Stack 14 43 84 16 33 97 13 72 53 99 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 84 43 14 84 13 53 06 99 72 43 33 64 97 51 25 Stack 14 14 43 84 16 33 97 13 72 53 99 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 13 16 43 14 84 13 53 06 99 72 43 33 64 97 51 25 Stack 14 43 84 16 33 97 13 72 53 99 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 53 16 43 14 84 13 53 06 99 72 43 33 64 97 51 25 Stack 14 43 84 16 33 97 13 13 72 53 99 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 16 43 14 84 13 53 06 99 72 43 33 64 97 51 25 Stack 14 43 84 16 33 97 13 72 53 99 53 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 99 72 43 14 84 13 53 16 99 72 43 33 64 97 51 25 Stack 14 43 84 16 6 33 97 13 72 53 99 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 72 43 14 84 13 53 16 99 72 43 33 64 97 51 25 Stack 14 43 84 16 33 97 13 72 53 99 99 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 43 14 84 13 53 16 99 72 43 33 64 97 51 25 Stack 14 43 84 16 33 97 13 72 72 53 99 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 33 97 14 84 13 53 16 99 72 43 33 64 97 51 25 Stack 14 43 43 84 16 33 97 13 72 53 99 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 64 97 14 84 13 53 16 99 72 43 33 64 97 51 25 Stack 14 43 84 16 33 33 97 13 72 53 99 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 97 14 84 13 53 16 99 72 43 33 64 97 51 25 Stack 14 43 84 16 33 97 13 72 53 99 64 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 14 84 13 53 16 99 72 43 33 64 97 51 25 Stack 14 43 84 16 33 97 97 13 72 53 99 64
Preorder Traversal with a Stack • Push the root onto the stack. • While the stack is not empty • pop the stack and visit it • push its two children 14 84 13 53 16 99 72 43 33 64 97 51 25 Stack 14 43 84 16 33 97 13 72 53 99 64