To understand recursion, you must understand recursion. I will show you 13 different ways to traverse a tree to compare recursive and iterative implementations. This way, we will kill two birds with one stone: **recursion and **data structures and algorithms.

“Bad programmers worry about the code. Good programmers worry about data structures and their relationships.”

Linus Torvalds

Recursion, iteration, and how to traverse a tree are useful skills to have and common in interview questions. Do not risk failing your next job interview because you didn’t take the time to read one article.

I assume you have basic coding skills and are familiar with stacks, queues, recursion, and loops. If this seems too advanced for you, check this article where I have listed some resources to get you started.

For every problem, I will provide also a link to Leetcode so that you can play around with my solution or write your own in your preferred programming language.

My code examples will be in C++. If you are not familiar with the APIs or syntax, that is fine. I am sure you will understand the ideas. That is the goal of this article.

What Is a Tree?

Image for post

N-ary tree

Trees are data structures that organize data hierarchically. They are defined **recursively **from a root node, that contains the data that needs to be stored and pointers to its children.

There are many types of trees well-suited for specific applications:

There is no point in storing information if you do not know how to access it.

For simplicity, I will focus on binary trees. You will take the code and generalize to trees with up to N children per node.

I will present you with 13 ways to traverse a tree and discuss them in detail. As usual, there will be challenges for you to cement your knowledge. I strongly recommend solving each problem on your own before reading my solution.

You are going to learn infinitely more by doing than by reading or watching.

13 Ways To Traverse a Tree

Most of the problems in this section will be on binary trees. We will define a node as follows:

Binary search node definition

Using a struct makes the fields public by default, which is enough for our purposes.

For the complexity analysis, we will assume that we will traverse a tree of height H that contains N nodes.

1. Pre-order Traversal — Recursive

Given the root of a binary tree, return the preorder traversal of its nodes’ values.

Image for post

You can play around with this problem here.

Solution

The pre-order, in-order, and post-order traversal of a tree are defined by the order in which we visit the root, left, and right children of a tree. The pre in pre-order refers to the root. In a preorder traversal of a tree, we first visit the root node, then the left node, and finally the right node.

You can translate this into recursive code very easily. Your function will receive a node, starting with the root of the tree. You need to define:

  • Base condition: As long as the node is not null,
  • Recursive relationship: you process it (for instance, print its value), and then call the same function in the left and right children.

For the base condition, you have two alternatives. I have called them A and B in my code. Have a look at the code and try to figure out the pros and cons of each before you look at my comments.

#algorithms #coding #programming #recursion #trees

Recursion vs Iteration: 13 Ways to Traverse a Tree.
1.75 GEEK