Preorder Traversal of Binary Tree in Python Last Updated : 22 Feb, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Preorder traversal is defined as a type of tree traversal that follows the Root-Left-Right policy where:The root node of the subtree is visited first.Then the left subtree is traversed.At last, the right subtree is traversed.Consider the following tree: If we perform a preorder traversal in this binary tree, then the traversal will be as follows:Step-by-step approach:Step 1: At first the root will be visited, i.e. node 1.Step 2: After this, traverse in the left subtree. Now the root of the left subtree is visited i.e., node 2 is visited.Step 3: Again the left subtree of node 2 is traversed and the root of that subtree i.e., node 4 is visited.Step 4: There is no subtree of 4 and the left subtree of node 2 is visited. So now the right subtree of node 2 will be traversed and the root of that subtree i.e., node 5 will be visited.Step 5: The left subtree of node 1 is visited. So now the right subtree of node 1 will be traversed and the root node i.e., node 3 is visited.Step 6: Node 3 has no left subtree. So the right subtree will be traversed and the root of the subtree i.e., node 6 will be visited. After that there is no node that is not yet traversed. So the traversal ends.So the order of traversal of nodes is 1 -> 2 -> 4 -> 5 -> 3 -> 6.Examples of Preorder TraversalInput: Output: ABCExplanation: The Preorder traversal visits the nodes in the following order: Root, Left, Right. Therefore, we visit the root node A, then visit the left node B and lastly the right node CInput: Output: ABDECInput: NULL Output: Output is empty in this case. Algorithm for Preorder Traversal of Binary TreeThe algorithm for preorder traversal is shown as follows:Preorder(root):If root is NULL then returnProcess root (For example, print root’s data)Preorder (root -> left)Preorder (root -> right)Python Program to Implement Preorder Traversal of Binary TreeBelow is the code implementation of the preorder traversal: Python # Python program for preorder traversals # 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) # Driver code 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) # Function call print("Preorder traversal of binary tree is:") printPreorder(root) OutputPreorder traversal of binary tree is: 1 2 4 5 3 6 Complexity Analysis:Time Complexity: O(N) where N is the total number of nodes. Because it traverses all the nodes at least once.Auxiliary Space: O(1) if no recursion stack space is considered. Otherwise, O(h) where 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 logN (when the tree is a complete tree)Related Posts:Inorder Traversal of Binary Tree in PythonPostorder Traversal of Binary Tree in Python Comment More infoAdvertise with us Next Article Binary Tree in Python B brijkan3mz4 Follow Improve Article Tags : Python Python-DSA Practice Tags : python Similar Reads Postorder Traversal of Binary Tree in Python Postorder traversal is defined as a type of tree traversal which follows the Left-Right-Root policy such that for each node:The left subtree is traversed firstThen the right subtree is traversedFinally, the root node of the subtree is traversedConsider the following tree: If we perform a postorder t 3 min read Inorder Traversal of Binary Tree in Python Inorder traversal is defined as a type of tree traversal technique which follows the Left-Root-Right pattern, such that:The left subtree is traversed firstThen the root node for that subtree is traversedFinally, the right subtree is traversedConsider the following tree: If we perform an inorder trav 3 min read Binary Search Tree In Python A Binary search tree is a binary tree where the values of the left sub-tree are less than the root node and the values of the right sub-tree are greater than the value of the root node. In this article, we will discuss the binary search tree in Python.What is a Binary Search Tree(BST)?A Binary Searc 11 min read Binary Tree in Python Binary Tree is a non-linear and hierarchical data structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves.Introduction to Binary TreeRepresentation of Binar 9 min read Tree Traversal Techniques in Python A tree is a non-linear data structure or we can say that it is a type of hierarchical structure. It is like an inverted tree where roots are present at the top and leaf or child nodes are present at the bottom of the tree. The root node and its child nodes are connected through the edges like the br 5 min read Random Binary Tree Generator using Python A binary tree is a tree data structure where each node has at most two children, known as the left child and the right child. A random binary tree is a binary tree that is generated randomly with a certain number of nodes. Random binary trees are commonly used for testing algorithms and data structu 7 min read Like