DEV Community

Debesh P.
Debesh P.

Posted on

1 1 1 1 1

Preorder Traversal | GeeksforGeeks Beginner's DSA Sheet | Tree Data Structure | With Source Code

hola coders!

In this video, we will break down Preorder Traversal of a Binary Tree step by step and solve a real GeeksforGeeks problem to reinforce our understanding. Preorder Traversal follows the NLR (Node → Left → Right) approach and is one of the fundamental tree traversal techniques used in data structures and algorithms (DSA).

Binary Trees are one of the most crucial topics in Data Structures and Algorithms (DSA), frequently asked in coding interviews at FAANG and top tech companies. Understanding Preorder Traversal is essential because it lays the foundation for solving more complex tree-based problems.


Problem Link: https://www.geeksforgeeks.org/problems/preorder-traversal/1

Source Code: https://github.com/debeshp6/Gfg-Tutorials/blob/main/PreorderTraversal.java


Code



//Back-end complete function Template for Java
class Solution {
    // Function to return a list containing the preorder traversal of the tree.
    static ArrayList<Integer> preorder(Node root) {
        // write code here
        ArrayList<Integer> arr = new ArrayList<>();
        helper(root, arr); // recursion
        return arr;
    }


    // preorder traversal: root - left - right

    static void helper(Node root, ArrayList<Integer> arr) { // we are creating a helper function here
        if(root == null) return; // base case
        arr.add(root.data);
        helper(root.left, arr); // recursion
        helper(root.right, arr);

        // helper function is readyyyy !!!!
    }
}
Enter fullscreen mode Exit fullscreen mode

Time Complexity: O(n)


who should watch this?

  • Beginners & Intermediate Programmers – If you are new to trees, this video simplifies the concepts for you
  • Competitive Programmers – Improve your problem-solving skills with practical applications
  • Job Seekers & Interview Prep Enthusiasts – Preorder Traversal is commonly asked in coding interviews
  • DSA Enthusiasts – Strengthen your understanding of fundamental tree traversal techniques

Got any questions? Drop them in the comments below! I’ll be happy to help.

happy coding!

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Postmark Image

"Please fix this..."

Focus on creating stellar experiences without email headaches. Postmark's reliable API and detailed analytics make your transactional emails as polished as your product.

Start free

Join the Runner H "AI Agent Prompting" Challenge: $10,000 in Prizes for 20 Winners!

Runner H is the AI agent you can delegate all your boring and repetitive tasks to - an autonomous agent that can use any tools you give it and complete full tasks from a single prompt.

Check out the challenge

DEV is bringing live events to the community. Dismiss if you're not interested. ❤️