Member-only story

Binary tree fundamentals #2: Breadth first and depth first tree traversal

Code Canvas
3 min readNov 21, 2023

--

In my previous post, I talked about the concepts of a binary tree and its implementation in Java. In this post, I will continue to discuss about another important concept in binary tree data structure — tree traversal.

Table of contents

· Breadth first (or level order):
· Depth first:
Inorder (left — root — right)
Preorder (root — left — right)
Postorder (left — right — root)

Tree traversal is a process of going through the entire tree, visiting each node of a tree exactly once. We can use tree traversal to count the number of nodes, search for a node in the tree, list the labels of tree nodes etc. Tree traversal can be broadly categorized into 2 methods: breadth first and depth first.

Breadth first (or level order):

In breadth-first method, we start from the root, then explore all child nodes at the same level before moving to the next level.

Breadth first (or level order) tree traversal

--

--

Code Canvas
Code Canvas

Written by Code Canvas

Hi, I'm Hi Tran, a tech and personal growth enthusiast . I use this Medium to document my journey and share insights that may help you on your own path.

No responses yet