어흥
[해커랭크] Preorder, Inorder, Postorder traversal (C++) 본문
728x90
반응형
문제링크:
[Preorder] www.hackerrank.com/challenges/tree-preorder-traversal/problem?h_r=internal-search
[Inorder] www.hackerrank.com/challenges/tree-inorder-traversal/problem?h_r=internal-search
[Postorder] www.hackerrank.com/challenges/tree-postorder-traversal/problem?h_r=internal-search
1. 주의할 점
- 전위, 중위, 후위 순회의 출력 순서에 대해 알고 있어야 한다
2. 구현
[Preorder] : Root->Left->Right
void preOrder(Node *root) {
cout << root->data<<" ";
if(root->left!=NULL){
preOrder(root->left);
}
if(root->right!=NULL){
preOrder(root->right);
}
}
[Inorder] : Left->Root->Right
void InOrder(Node *root) {
if(root->left!=NULL){
preOrder(root->left);
}
cout << root->data<<" ";
if(root->right!=NULL){
preOrder(root->right);
}
}
[Postorder] : Left->Right->Root
void postOrder(Node *root) {
if(root->left!=NULL){
preOrder(root->left);
}
if(root->right!=NULL){
preOrder(root->right);
}
cout << root->data<<" ";
}
728x90
반응형
'알고리즘 > HackerRank' 카테고리의 다른 글
[해커랭크] Tree: Level Order Traversal (C++) (0) | 2021.01.20 |
---|---|
[해커랭크] Tree: Height of a Binary Tree (C++) (0) | 2021.01.20 |
[해커랭크] Find Merge Point of Two Lists (C++) (0) | 2021.01.15 |
[해커랭크] Delete duplicate-value nodes from a sorted linked list (C++) (0) | 2021.01.15 |
[해커랭크] Merge two sorted linked lists (C++) (2) | 2021.01.15 |
Comments