Preorder Traversal
Submissions: 21846   Accuracy:


  Difficulty: Basic   Marks: 1

Given a binary tree, print preorder traversal of it. The task is to complete the function preorder(), which accept root of the tree as argument.
For example: preorder traversal of below tree is "1 10 5 39"

     /     \
   10     39

The first line of input contains T, denoting the number of testcases. Each testcase contains two lines. The first line contains number of edges in a tree. The second line contains number of edges+1 number of nodes. The struct Node has a data part which stores the data, pointer to left child and pointer to right child.

The function should print preorder traversal.

User Task:
Since this is a functional problem you don't have to worry about input, you just have to complete the function preorder() that prints the data in preorder way. The newline is automatically appended by the driver code.

1 <= T <= 30
1 <= Number of nodes <= 100
1 <= Data of a node <= 1000


1 2 R 1 3 L
10 20 L 10 30 R 20 40 L 20 60 R

1 3 2
10 20 40 60 30

Testcase 1
: There are 3 nodes in tree and the preorder traversal would give 1 3 2.


** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa
Author: kartik

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.