HomeArticles

Binary Tree Traversal Orders

04 September, 2020 - 1 min read

Random bits about binary trees traversal orders.

Tree #1

    1
   / \
  2   2
 / \ / \
3  4 4  3

BFS             [1, 2, 2, 3, 4, 4, 3]
DFS (preorder)  [1, 2, 3, 4, 2, 4, 3]
DFS (inorder)   [3, 2, 4, 1, 4, 2, 3]
DFS (postorder) [3, 4, 2, 4, 3, 2, 1]

Tree #2

    5
   / \
  2   6
 / \   \
1   4   8
   /   / \
  3   7   9

BFS             [5, 2, 6, 1, 4, 8, 3, 7, 9]
DFS (preorder)  [5, 2, 1, 4, 3, 6, 8, 7, 9]
DFS (inorder)   [1, 2, 3, 4, 5, 6, 7, 8, 9]
DFS (postorder) [1, 3, 4, 2, 7, 9, 8, 6, 5]

© 2019 - 2020