Thursday, January 14, 2016

Tree: Preorder Traversal - Hacker Rank Solution

You are given a pointer to the root of a binary tree; print the values in preorder traversal.
You only have to complete the function.

Input Format
You are given a function,
void Preorder(node *root) {

}
Output Format
Print the values on a single line separated by space.
Sample Input
     3
   /   \
  5     2
 / \    /
1   4  6
Sample Output
3 5 1 4 2 6 
---------------------------------------------------------------------------------
 /* you only have to complete the function given below.  
Node is defined as  

struct node
{
    int data;
    node* left;
    node* right;
};

*/

void Preorder(node *root) {
    struct node *temp;
    temp=root;
    if(temp!=NULL)
      {  
        printf("%d ",temp->data);
        Preorder(temp->left);
        Preorder(temp->right);
    }

}
----------------------------------------------------------------------------------
Powered by Blogger.