Tree-Preorder Traversal

Problem Information:

problem's link

AC date : 2017-04-24

Category : data structure->tree


Question

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

Alt text

Sample Output

3 5 1 4 2 6

Answer

C++

/* 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);
}

上一篇: Maximum Element
下一篇: Print the Elements of a Linked List

发布时间:
2017-04-24 23:10
分类:
标签: