Preorder, Inorder, Postorder Traversal with Binary Trees: Leetcode Solution Provided in Java

Preorder, Inorder, Postorder Traversal with Binary Trees: Leetcode Solution Provided in Java

Preorder, Inorder, and Postorder are three of the four ways to traverse a binary tree. These approaches are the three ways to do a depth-first search of a binary tree.

Preorder Traversal - involves accessing the root first, then visiting its left child node and right child node afterward.

Inorder Traversal - involves visiting the left tree nodes first until reaching a leaf node, then accessing the current root, and lastly visiting the root's right child node.

Postorder Traversal is when the left is visited, followed by the right until reaching a leaf node, the accessing current root node.

Let's Look at the Approach More In-Depth And Solve Preorder Traversal

The preorder traversal aims to print each node's value in the binary search tree. As stated above, we print the root first, then visit the left node and the right node, but how? If you are familiar with recursion, then the solution is quite simple.

💡
If you're not familiar with recursion, you can read the article I wrote on recursion here: Recursion

To solve the Leetcode problem, Binary Tree Preorder Traversal, first you'll need to initialize an array list to store the result. This array will be returned once we finish traversing each node of the tree. Before we begin the traversal let's take a look at how to implement the helper function that will be used to traverse the tree.

Our helper function requires two params; a root node and an array. Since our helper function will be recursive, let's figure out the base case. How do we tell our function to stop? Well, you can't traverse any further if you have an empty node so we write our base case like so:

if(root is null) end the function

This is to help prevent a stack overflow exception from occurring. Next, we add the value of the root to the array list we created. Then, call our helper function(recurse) to traverse the root's left child node and finally its right child node.

Once all nodes have been visited, our array is returned in the main solution function. Below is the solution in both pseudocode and Java.

Binary Tree Preorder Traversal (easy)

function preorderTraversal(root):
    // Create an empty list to store the result
    answer = new ArrayList()

    // Start the preorder traversal from the root using a helper function
    preorder(answer, root)

    // Return the list containing the preorder traversal result
    return answer

function preorder(answer, root):
    // If the current node is null, return
    if root is null:
        return

    // Add the value of the current node to the result list
    answer.add(root.val)

    // Recursively traverse the left subtree
    preorder(answer, root.left)

    // Recursively traverse the right subtree
    preorder(answer, root.right)
class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> answer = new ArrayList<>();

        preorder(answer, root);

        return answer;
    }

    public void preorder(List<Integer> answer, TreeNode root) {
        if(root == null) return;

        answer.add(root.val);
        preorder(answer, root.left);
        preorder(answer, root.right);
    }
}

Now that you know how to do preorder traversal. Try doing the same for inorder and postorder traversal. I have provided the solution for both in-order and postorder as well. It's quite simple once you know how to do one of the three traversals.

Binary Tree Inorder Traversal

Solution

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> answer = new ArrayList<>();

        inorder(answer, root);
        return answer;
    }

    public void inorder(List<Integer> answer, TreeNode root) {
       if(root == null) return;

       inorder(answer, root.left);
       answer.add(root.val);
       inorder(answer, root.right);
    } 
}

Binary Tree Postorder Traversal

Solution

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> answer = new ArrayList<>();

        postorder(answer, root);

        return answer;
    }

    public void postorder(List<Integer> answer, TreeNode root) {
        if(root == null) return;

        postorder(answer, root.left);
        postorder(answer, root.right);
        answer.add(root.val);
    }
}

Additional details about preorder, inorder and postorder traversal:

  • All three methods are depth-first traversal techniques

  • They are applied to binary trees, trees with at most two child nodes per parent

  • Different traversal orders have different results and are suited for different tasks.

  • Recursive algorithms are commonly used for implementing these traversals

  • iterative algorithms that use stacks or queues can also be used for traversal without recursion

I hope this article was helpful to you. If you have any questions, please leave them in the comment section.

Did you find this article valuable?

Support Karelle Hofler by becoming a sponsor. Any amount is appreciated!