Visit every node of the tree from top level (root) to the bottom level.

For example, for this tree:

The output will be:

Thinking:

  1. If we know the nodes from the upper level, it’s easy to reach the nodes at the lower level.

When solving a search problem, we should think of binary search whenever the data set is sorted in some way.

Even if the dataset is not sorted, we should still think of binary search if the data is sortable. …

This article talks about recursive solutions of binary tree traversal.

Preorder

Thinking:

  1. Start from the root, visit the parent node before going into any sub-tree.
  2. If the parent has only left child, go to the left.
  3. If the parent has only right child, go to the right.
  4. If the parent…

Binary Tree is a special tree structure where every node has no more than two children. Example:

A binary tree example

Preorder

Visit the parent node as the first.

  1. Visit the parent node
  2. Visit the left tree
  3. Visit the right tree

Note: it’s visiting the left tree & right tree, not just the left…

Nardis

A music lover who can also writes code.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store