Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


1. Overview

Reversing a binary tree is one of the problems that we might be asked to solve during a technical interview.

In this quick tutorial, we’ll see a couple of different ways of solving this problem.

2. Binary Tree

A binary tree is a data structure in which each element has at most two children, which are referred to as the left child and the right child. The top element of the tree is the root node, whereas the children are the interior nodes.

However, if a node has no child, it's called a leaf.

Having said that, let's create our object that represents a node:

public class TreeNode {

    private int value;
    private TreeNode rightChild;
    private TreeNode leftChild;

    // Getters and setters


Then, let's create our tree that we'll be using in our examples:

    TreeNode leaf1 = new TreeNode(1);
    TreeNode leaf2 = new TreeNode(3);
    TreeNode leaf3 = new TreeNode(6);
    TreeNode leaf4 = new TreeNode(9);

    TreeNode nodeRight = new TreeNode(7, leaf3, leaf4);
    TreeNode nodeLeft = new TreeNode(2, leaf1, leaf2);

    TreeNode root = new TreeNode(4, nodeLeft, nodeRight);

In the previous method, we created the following structure:


By reversing the tree from left to right, we'll end up having the following structure:


3. Reversing the Binary Tree

3.1. Recursive Method

In the first example, we'll use recursion to reverse the tree.

First of all, we'll call our method using the tree's root, then we'll apply it on the left and the right children respectively until we reach the tree's leaves:

public void reverseRecursive(TreeNode treeNode) {
    if(treeNode == null) {

    TreeNode temp = treeNode.getLeftChild();


3.2. Iterative Method

In the second example, we'll reverse the tree using an iterative approach. For that, we're going to use a LinkedList, which we initialize with the root of our tree.

Then, for every node we poll from the list, we add its children to that list before we permutate them.

We keep adding and removing from the LinkedList until we reach the tree's leaves:

public void reverseIterative(TreeNode treeNode) {
    List<TreeNode> queue = new LinkedList<>();

    if(treeNode != null) {

    while(!queue.isEmpty()) {
        TreeNode node = queue.poll();
        if(node.getLeftChild() != null){
        if(node.getRightChild() != null){

        TreeNode temp = node.getLeftChild();

4. Conclusion

In this quick article, we explored the two ways of reversing a binary tree. We have started by using a recursive method to reverse it. Then, we ended up using an iterative way to achieve the same result.

The complete source code of these examples and unit test cases can be found over on Github.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:


Generic footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!