TreesAnimated
Preorder Traversal
Node, left, right. Visits parents before children.
Time
O(n)
Extra space
O(h)
Use it for
Copying or serialising a tree.
How it works
- Visit the node first.
- Then traverse its left subtree.
- Then traverse its right subtree.
- Parents always come before their children, so it can be used to copy or save a tree.
Watch it run
Loading animation…