Depth First Traversal
Depth First Traversal in Java
Depth first traversal means we start at the root node and explores as far as possible along each branch before backtracking. There are three depth first traversals.

Depth First Traversal
- Pre-order : In preorder traversal, we visit each node before visiting any of its children. We follow the pattern : Root -> Left child -> Right child
- In-order : In inorder traversal, we visit each node after visiting its left child, but before visiting its right child. We follow the pattern : Left child -> Root ->Right child
- Post-order : In postorder traversal, we visit each node after visiting both of its children. We follow the pattern : Left child -> Right child -> Root node

Algorithm:
Preorder:
- Visit the root.
- Traverse the left subtree.
- Traverse the right subtree.
Inorder:
- Traverse the left subtree.
- Visit the root.
- Traverse the right subtree.
Postorder:
- Traverse the left subtree.
- Traverse the right subtree.
- Visit the root.
Code For Binary Search Tree
Run
import java.util.*; public class Main { //Node class public static class Node { int data; Node left; Node right; public Node (int data) { this.data = data; this.left = null; this.right = null; } } //Binary tree class to create a binary tree public static class BinaryTree { Node root; public BinaryTree () { this.root = null; } } //Preorder traversal public static void preOrder (Node node) { //Base case if (node == null) { return; } //Print root data System.out.print (node.data + " "); //Go to left subtree preOrder (node.left); //Go to right subtree preOrder (node.right); } //Inorder traversal public static void inOrder (Node node) { //Base case if (node == null) { return; } //Go to left subtree inOrder (node.left); //Print root data System.out.print (node.data + " "); //Go to right subtree inOrder (node.right); } //Postorder traversal public static void postOrder (Node node) { //Base case if (node == null) { return; } //Go to left subtree postOrder (node.left); //Go to right subtree postOrder (node.right); //Print root data System.out.print (node.data + " "); } public static void main (String[]args) throws Exception { //Construct binary tree BinaryTree bt = new BinaryTree (); bt.root = new Node (10); bt.root.left = new Node (20); bt.root.right = new Node (30); bt.root.left.left = new Node (40); bt.root.left.right = new Node (50); System.out.println ("Preorder Traversal :"); preOrder (bt.root); System.out.println ("\nInorder Traversal :"); inOrder (bt.root); System.out.println ("\nPostorder Traversal :"); postOrder (bt.root); } }
Output :
Preorder Traversal : 10 20 40 50 30 Inorder Traversal : 40 20 50 10 30 Postorder Traversal : 40 50 20 30 10
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal Line by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric – C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
Binary Search Trees
Traversals
- Traversal in Trees
- Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
- Tree Traversals: Depth First Search (DFS) : C | C++ | Java
- Construct a Binary Tree from Postorder and Inorder
B – Trees
AVL Trees
- AVL Trees
Complete Programs for Trees
- Depth First Traversals – C | C++ | Java
- Level Order Traversal – C | C++ | Java
- Construct Tree from given Inorder and Preorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Inorder traversals – C | C++ | Java
- Construct Tree from given Postorder and Preorder traversal – C | C++ | Java
- Find size of the Binary tree – C | C++ | Java
- Find the height of binary tree – C | C++ | Java
- Find maximum in binary tree – C | C++ | Java
- Check whether two tree are identical- C| C++| Java
- Spiral Order traversal of Tree- C | C++| Java
- Level Order Traversal LIne by Line – C | C++| Java
- Hand shaking lemma and some Impotant Tree Properties.
- Check If binary tree if Foldable or not.- C| C++| Java
- check whether tree is Symmetric C| C++| Java.
- Check for Children-Sum in Binary Tree- C|C++| Java
- Sum of all nodes in Binary Tree- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java