Preorder Traversal of Binary Tree Last Updated : 28 Mar, 2025 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice Preorder traversal is a tree traversal method that follows the Root-Left-Right order:The root node of the subtree is visited first.Next, the left subtree is recursively traversed.Finally, the right subtree is recursively traversed.How does Preorder Traversal work?Key Properties: Used in expression trees to generate prefix notation.Traverse the root node first.Examples:Input: Output: 1 2 3Explanation: The Preorder Traversal visits the nodes in the following order: Root, Left, Right. Therefore, we visit the root node 1, then the left node 2 and lastly the right node 3.Input :Output: 1 2 4 5 3 6Explanation: Preorder Traversal (Root → Left → Right). Visit 1 → 2 → 4 → 5 → 3 → 6, resulting in 1 2 4 5 3 6. Algorithm :If the root is NULL, return; Process the root node (e.g., print its value); Recursively traverse the left subtree; Recursively traverse the right subtree. C++ #include <bits/stdc++.h> using namespace std; // Structure of a Binary Tree Node struct Node { int data; struct Node *left, *right; Node(int v) { data = v; left = right = nullptr; } }; // Function to print Preorder traversal void printPreorder(struct Node* node) { if (node == nullptr) return; // Deal with the node cout << node->data << " "; // Recursion on left subtree printPreorder(node->left); // Recursion on right subtree printPreorder(node->right); } int main() { struct Node* root = new Node(1); root->left = new Node(2); root->right = new Node(3); root->left->left = new Node(4); root->left->right = new Node(5); root->right->right = new Node(6); printPreorder(root); return 0; } C #include <stdio.h> #include <stdlib.h> // Structure of a Binary Tree Node struct Node { int data; struct Node* left; struct Node* right; }; // Function to create a new node struct Node* newNode(int v) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = v; node->left = NULL; node->right = NULL; return node; } // Function to print preorder traversal void printPreorder(struct Node* node) { if (node == NULL) return; // Deal with the node printf("%d ", node->data); // Recur on left subtree printPreorder(node->left); // Recur on right subtree printPreorder(node->right); } int main() { struct Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->right = newNode(6); printPreorder(root); printf("\n"); return 0; } Java import java.util.*; // Structure of a Binary Tree Node class Node { int data; Node left, right; Node(int v) { data = v; left = right = null; } } // Class for BinaryTree class BinaryTree { Node root; // Function to print preorder traversal public static void printPreorder(Node node) { if (node == null) return; // First deal with the node System.out.print(node.data + " "); // Then recur on left subtree printPreorder(node.left); // Finally recur on right subtree printPreorder(node.right); } } class GFG { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.right = new Node(6); BinaryTree.printPreorder(tree.root); } } Python # Structure of a Binary Tree Node class Node: def __init__(self, v): self.data = v self.left = None self.right = None # Function to print preorder traversal def printPreorder(node): if node is None: return # Deal with the node print(node.data, end=' ') # Recur on left subtree printPreorder(node.left) # Recur on right subtree printPreorder(node.right) if __name__ == '__main__': root = Node(1) root.left = Node(2) root.right = Node(3) root.left.left = Node(4) root.left.right = Node(5) root.right.right = Node(6) printPreorder(root) C# using System; // Structure of a Binary Tree Node public class Node { public int data; public Node left, right; public Node(int v) { data = v; left = right = null; } } public class BinaryTree { // Function to print preorder traversal public static void printPreorder(Node node) { if (node == null) return; // Deal with the node Console.Write(node.data + " "); // Recur on left subtree printPreorder(node.left); // Recur on right subtree printPreorder(node.right); } public static void Main() { Node root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.right = new Node(6); printPreorder(root); } } JavaScript // Structure of a Binary Tree Node class Node { constructor(v) { this.data = v; this.left = null; this.right = null; } } // Function to print preorder traversal function printPreorder(node) { if (node === null) { return; } // Deal with the node console.log(node.data); // Recur on left subtree printPreorder(node.left); // Recur on right subtree printPreorder(node.right); } function main() { const root = new Node(1); root.left = new Node(2); root.right = new Node(3); root.left.left = new Node(4); root.left.right = new Node(5); root.right.right = new Node(6); printPreorder(root); } main(); Output1 2 4 5 3 6 Time Complexity: O(n)Auxiliary Space: O(h), h is the height of the treeIn the worst case, h can be the same as n (when the tree is a skewed tree)In the best case, h can be the same as log n (when the tree is a complete tree)Related Articles:Types of Tree TraversalIterative preorder traversalCheck if given array can represent preorder traversal of BSTPreorder from inorder and postorder traversalsFind nth node in preorder traversal of a binary treePreorder traversal of an N-ary tree Comment More infoAdvertise with us Next Article Preorder Traversal of Binary Tree A animeshdey Follow Improve Article Tags : Tree DSA Binary Tree Tree Traversals Practice Tags : Tree Similar Reads Postorder Traversal of Binary Tree Postorder traversal is a tree traversal method that follows the Left-Right-Root order:The left subtree is visited first.The right subtree is visited next.The root node is processed last.How does Postorder Traversal work?Key Properties:It is used for tree deletion because subtrees are deleted before 5 min read Mix Order Traversal of a Binary Tree Given a Binary Tree consisting of N nodes, the task is to print its Mix Order Traversal. Mix Order Traversal is a tree traversal technique, which involves any two of the existing traversal techniques like Inorder, Preorder and Postorder Traversal. Any two of them can be performed or alternate levels 13 min read Inorder Traversal of Binary Tree Inorder traversal is a depth-first traversal method that follows this sequence:Left subtree is visited first.Root node is processed next.Right subtree is visited last.How does Inorder Traversal work?Key Properties:If applied to a Binary Search Tree (BST), it returns elements in sorted order.Ensures 5 min read Double Order Traversal of a Binary Tree Given a Binary Tree, the task is to find its Double Order Traversal. Double Order Traversal is a tree traversal technique in which every node is traversed twice in the following order: Visit the Node.Traverse the Left Subtree.Visit the Node.Traverse the Right Subtree.Examples:Input: Output: 1 7 4 4 6 min read Triple Order Traversal of a Binary Tree Given a Binary Tree, the task is to find its Triple Order Traversal. Triple Order Traversal is a tree traversal technique in which every node is traversed thrice in the following order: Visit the root nodeTraverse the left subtreeVisit the root nodeTraverse the right subtreeVisit the root node.Examp 7 min read Like