Recursion vs Iteration: 13 Ways to Traverse a Tree.

Recursion vs Iteration: 13 Ways to Traverse a Tree.

To understand recursion, you must understand recursion. I will show you 13 different ways to traverse a tree to compare recursive and iterative.

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.


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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Coding 101: Programming Language Building Blocks

This article will introduce the concepts and topics common to all programming languages, that beginners and experts must know!

Day 15 of 30 Ruby Coding Challenge - Fibonacci Sequence Recursively

We're going to solve the famous Fibonacci sequence recursively in Ruby. This is not a better strategy than the previous one, this will be another option. Today, we’ll solve the previous Fibonacci problem using the dreaded recursion approach. A little bit of theory was explored in this video, and today, I’ll straight to the point

While You Don't Understand Recursion, Read Recursion: by Randy Taylor

Recursion is the one idea I constantly use while I solve coding problems. Most of the time I don’t start by thinking “RECURSION WILL SOLVE THIS!”. However recursion just ends up being the logical way to reach an answer.

13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.

What Is Code Golfing And Biggest Such Tournaments

Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that implements a certain algorithm. Playing code golf is known as "golf scripting". Code golf challenges and tournaments may also be named with the ... Some code golf questions, such as those posed on general programming ...