Preorder Traversal is a method to traverse a tree such that for each node, you first visit the node itself, then traverse its left subtree, and finally traverse its right subtree.
Input:
Output: [1, 2, 3] Explanation: 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.
The main idea is to traverse the tree recursively, starting from the root node, first visit the root node itself, then completely traverse the left subtree, and finally completely traverse the right subtree.
How does Preorder Traversal work?
C++
#include<iostream>#include<vector>usingnamespacestd;//Node StructureclassNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=right=NULL;}};voidpreOrder(Node*node,vector<int>&res){if(node==nullptr)return;// Visit the current node firstres.push_back(node->data);// Traverse the left subtreepreOrder(node->left,res);// Traverse the right subtreepreOrder(node->right,res);}intmain(){// Create binary tree// 1// / \ // 2 3// / \ \ // 4 5 6Node*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);vector<int>res;preOrder(root,res);for(intnode:res)cout<<node<<" ";return0;}
C
#include<stdio.h>#include<stdlib.h>// Node StructurestructNode{intdata;structNode*left;structNode*right;};// Function to create a new nodestructNode*newNode(intx){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=x;node->left=NULL;node->right=NULL;returnnode;}voidpreOrder(structNode*node){if(node==NULL)return;// Visit the current node firstprintf("%d ",node->data);// Traverse the left subtreepreOrder(node->left);// Traverse the right subtreepreOrder(node->right);}intmain(){// Create binary tree// 1// / \ // 2 3// / \ \ // 4 5 6structNode*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);preOrder(root);printf("\n");return0;}
Java
importjava.util.ArrayList;//Node StructureclassNode{intdata;Nodeleft,right;Node(intv){data=v;left=right=null;}}classGFG{publicstaticvoidpreOrder(Nodenode,ArrayList<Integer>res){if(node==null)return;// Visit the current node firstres.add(node.data);// Traverse the left subtreepreOrder(node.left,res);// Traverse the right subtreepreOrder(node.right,res);}publicstaticvoidmain(String[]args){// Create binary tree// 1// / \// 2 3// / \ \// 4 5 6Noderoot=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);ArrayList<Integer>result=newArrayList<>();preOrder(root,result);for(intval:result){System.out.print(val+" ");}}}
Python
#Node StructureclassNode:def__init__(self,data):self.data=dataself.left=Noneself.right=NonedefpreOrder(node,res):ifnotnode:return# Visit the current node firstres.append(node.data)# Traverse the left subtreepreOrder(node.left,res)# Traverse the right subtreepreOrder(node.right,res)if__name__=="__main__":# Create binary tree# 1# / \# 2 3# / \ \# 4 5 6root=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)result=[]preOrder(root,result)print(*result)
C#
usingSystem;usingSystem.Collections.Generic;//Node StructureclassNode{publicintdata;publicNodeleft,right;publicNode(intv){data=v;left=right=null;}}classGFG{publicstaticvoidpreOrder(Nodenode,List<int>res){if(node==null)return;// Visit the current node firstres.Add(node.data);// Traverse the left subtreepreOrder(node.left,res);// Traverse the right subtreepreOrder(node.right,res);}staticvoidMain(){// Create binary tree// 1// / \// 2 3// / \ \// 4 5 6Noderoot=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);List<int>result=newList<int>();preOrder(root,result);foreach(intvalinresult)Console.Write(val+" ");}}
JavaScript
//Node StructureclassNode{constructor(data){this.data=data;this.left=null;this.right=null;}}functionpreOrder(node,res){if(!node)return;// Visit the current node firstres.push(node.data);// Traverse the left subtreepreOrder(node.left,res);// Traverse the right subtreepreOrder(node.right,res);}// Driver code// Create binary tree// 1// / \// 2 3// / \ \// 4 5 6letroot=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);letresult=[];preOrder(root,result);console.log(...result);
Output
1 2 4 5 3 6
Time Complexity: O(n) Auxiliary Space: O(h), h is the height of the tree
In 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)