Menu
Coddy logo textTech

Pre-order Traversal

Lesson 7 of 13 in Coddy's Binary Tree - Data Structures Series #3 course.

challenge icon

Challenge

Easy

Pre-order traversal:

  1. Visit the root
  2. Traverse the left subtree
  3. Traverse the right subtree

Add to BinaryTree the method preOrderPrint that prints the values in pre-order, each followed by a space.

Try it yourself

#include <stdio.h>
#include <string.h>
#include "binarytree.h"

int main() {
    char buf[4096];
    if (!fgets(buf, sizeof(buf), stdin)) buf[0] = '\0';
    buf[strcspn(buf, "\r\n")] = '\0';
    BinaryTree bt;
    BinaryTree_init(&bt);
    BinaryTree_buildTree(&bt, buf);
    BinaryTree_preOrderPrint(&bt);
    return 0;
}

All lessons in Binary Tree - Data Structures Series #3