HomeData StructuresBinary Tree Traversals in Kotlin - PreOrder, InOrder and PostOrder Traversals

Binary Tree Traversals in Kotlin – PreOrder, InOrder and PostOrder Traversals

-

Binary Tree Traversals are most common Tree Problems in an Interview as well as Competitive Programming. They can be efficient while searching, inserting and deleting compared to Linear Data structures (like Array, Linked Lists, Stacks, etc).

Trees can be traversed in many ways. Understanding traversing will help us solve complex searching in an efficient way with less time complexity.

While going through LeetCode Explore section of Binary Trees, I decided to document the Traversing of a Binary Tree so that it can be useful for myself while revisiting as well as it can be helpful for to our Coding Community.

A Binary Tree is a tree which has at-most two children. In this article we will see how we can implement Depth First Binary Tree Traversals along with Kotlin Examples.

Depth First Traversals:

  1. PreOrder Traversal
  2. InOrder Traversal
  3. PostOrder Traversal

Let us take an example Binary Tree and carefully look at each of the above traversals so that we can visualise and understand what happens during Tree Traversal.

Here is a Binary Tree formed with elements – 1,2,3,4,5.

Creating a TreeNode and a Binary Tree

Let us try to create the above Binary Tree. Firstly we create a TreeNode which will help creating our Binary Tree. Our function initializeBinaryTree() will be called whenever we need to create a Binary Tree.

Now let us see each Depth-First traversals in detail.

PreOrder Traversal

In PreOrder Traversal, each node is processed before its child subtrees. That is why the name – PreOrder.

Here the order of traversal for the above example will be as follows: 1 2 4 5 3.

Let us now see how we can achieve the same through Recursion.

Recursive:

As we can see, the recursive solution is Trivial and less asked in the interviews. Let us now see how we can achieve the same through Iterative Solution.

Iterative:

The code is commented wherever it is necessary. Feel free to leave comments in case of any queries.

InOrder Traversal

Here, the root node is processed in between left and right sub trees. Hence the name InOrder Traversal. The order of output using the above binary tree will be: 4 2 5 1 3

Now let us see the recursive solution for InOrder Traversal

Recursive

Here is the iterative Solution:

Iterative

Post-Order Traversal

In PostOrder Traversal, the parent node will be processed after both child subtrees are processed.

The output of the above binary tree if processed using PostOrder Traversal will be: 4 5 2 3 1.

Here are the Recursive and iterative Solutions for PostOrder:

Recursive:

Iterative:

Hope you find this Binary Tree Traversals in Kotlin article useful. Here is the GitHub Repo Link where the code is committed. Drop a Star if you like it. See you guys in our next article.

Vamsi Tallapudi
Architect Technology at Cognizant | Full Stack Engineer | Technical Blogger | AI Enthusiast

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

LATEST POSTS

Best Time to Buy and Sell Stock – Day 7 | 100 Days of Code

Welcome to Day 7 of 100 Days of Code where today we solve yet another most frequently asked easy array problem called - Best Time...

Contains Duplicate – Day 8 | 100 Days of Code

Welcome to Day 8 of 100 Days of Code and today we are going to solve yet another easy-level problem - Contains Duplicate as part...

Two Sum – Day 6 | 100 Days of Code

Welcome to Day 6 of 100 Days of Code where we solve the most frequently asked Easy level Array problem - Two Sum. Let us...

Arrays for Beginners: Unveiling the Power of Data Structures

Hello, Programmers 🌟 Welcome to Coderefer! Let's explore the world of arrays together. Arrays are like handy data structures that help you manage and work...
Exit mobile version