Visit every node of the tree from top level (root) to the bottom level.
For example, for this tree:

The output will be:
1, 2, 3, 4, 5, 6, 7Thinking:
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. It depends on if sorting the data first will make a better & faster solution.
Any dataset is sortable if you can directly compare any two items from the dataset, i.e., we should be able to decide which one is “smaller” or which one is “larger”. Examples:
This article talks about recursive solutions of binary tree traversal.
Preorder
Thinking:
Binary Tree is a special tree structure where every node has no more than two children. Example:

Preorder
Visit the parent node as the first.
Note: it’s visiting the left tree & right tree, not just the left node & right node.
def preorder_traversal(root):
if not root:
return
visit(root)
preorder_traversal(root.left)
preorder_traversal(root.right)If you print out the sequence, the root node must be the first. For the above example, it will be:
1, 2, 4, 5, 3, 6, 7Inorder
Visit the parent node in the middle.
A music lover who can also writes code.