Trees Flashcards
1
Q
InOrder(root) visits nodes in what order?
_2_ / \ 20 5 / \ / \ 22 40 25 15
A
22, 20, 40, 2, 25, 5, 15 InOrder Algorithm: - Traverse Left, InOrder(left) - Visit Root - Traverse Right, InOrder(right)
In in-order we visit left -> root -> right
2
Q
PreOrderTraversal(root) visits nodes in what order?
_2_ / \ 20 5 / \ / \ 22 40 25 15
A
2, 20, 22, 40, 5, 25, 15 PreOrder Algorithm: - Visit Root - Traverse Left, PreOrder(left) - Traverse Right, PreOrder(right)
In pre-order, we visit the root first
- root => left => right
3
Q
PostOrderTraversal(root) visits nodes in what order?
_2_ / \ 20 5 / \ / \ 22 40 25 15
A
22, 40, 20, 25, 15, 5, 2
PostOrder Algorithm:
- Traverse Left, PreOrder(left)
- Traverse Right, PreOrder(right)
- Visit Root
In post-order, we visit root last
- left => right => root
4
Q
What are the advantages depth first traversal over breadth first?
A
- Depth first on a binary tree generally requires less memory than breadth first
- Depth first can be easily implemented with recursion
5
Q
What are the advantages/disadvantages of breadth first traversal over depth-first?
A
Breadth first traversal finds the shortest path to a node
BFS requires more memory than DFS