Tree: Preorder Traversal

PROBLEM :

Complete the preOrder function in your editor below, which has  parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values.

Input Format

Our hidden tester code passes the root node of a binary tree to your preOrder function.

Output Format

Print the tree's preorder traversal as a single line of space-separated values.

Sample Input

4
       /   \
     2     7
    / \   /
  1   3 6

output : 4 2 1 3 7 6
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/* 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) {
 
    if(root==NULL)
        return ;
 
    cout<<root->data<<" " ;
    preOrder(root->left) ;
    preOrder(root->right) ;

}


---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )