![]() Since the left subtree of the subtree rooted 1 is traversed, so now traverse the right subtree.Īfter processing right subtree traversal of the tree rooted by 1 we will have traversal: 7 1 0 3 2 5 4 6 Also, 0 has no subtree, so this subtree rooted by 0 is traversed totally. Since it's a leaf node we can print it as there is no more left subtree. ![]() Then traverse the left subtree of this (rooted by 0) Now to traverse the subtree rooted by 1, it again follows the same set of rules Then Traverse the left subtree (Subtree rooted by 1) So let's traverse the below tree using preorder traversal. Of course, while traversing the subtrees we will follow the same order Pre means before (first/initially) and that's why root is being traversed first and then the subtrees. The basic concept for preorder traversal lies in its name itself. If we classify tree traversals, preorder traversal is one of the traversal techniques which is based on depth-first search traversal. In this article, we are going to find what preorder traversal of a Binary Tree is and how to implement preorder traversal using recursion? We have provided the implementation both in C & C++.
0 Comments
Leave a Reply. |