Programming/C C++

PreOrder/InOrder/PostOrder

728x90

 

https://www.hackerrank.com/challenges/tree-preorder-traversal/problem

 

Tree: Preorder Traversal | HackerRank

Print the preorder traversal of a binary tree.

www.hackerrank.com

PreOrder

VLR 순서로 트리를 순회하는 방식; prefix

void preOrder( struct node *root) {
    if(root){
        printf("%d ", root->data);
        preOrder(root->left);
        preOrder(root->right);
    }

}

 

cf.

InOrder

LVR 순서로 트리를 순회하는 방식; infix

void inOrder( struct node *root) {
    if(root){
        inOrder(root->left);
        printf("%d", root->data);
        inOrder(root->right);
    }
}

PostOrder

LRV 순서로 트리를 순회하는 방식; postfix

void postOrder( struct node *root) {
    if(root){
        postOrder(root->left);
        postOrder(root->right);
        printf("%d ",root->data);
    }
}

 

 

SMALL

'Programming > C C++' 카테고리의 다른 글

Queue using Two Stacks  (0) 2020.06.14
Mini-Max Sum  (0) 2020.06.14
Merge two sorted linked lists  (0) 2020.06.08
Balanced Brackets  (0) 2020.06.07
Cycle Detection  (0) 2020.05.29