Hackerrank | Tree: Preorder Traversal
                                            
                                                
                                                
                                                
                                                
                                                
                                                 9737 단어  hackerrank2020SISSC2020
                    
Link
https://www.hackerrank.com/challenges/tree-preorder-traversal/problem
Problem

preOrder 함수 완성시키기
왼쪽 > 오른쪽 순으로 읽기
Code
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct node {
    
    int data;
    struct node *left;
    struct node *right;
  
};
struct node* insert( struct node* root, int data ) {
        
    if(root == NULL) {
    
        struct node* node = (struct node*)malloc(sizeof(struct node));
        node->data = data;
        node->left = NULL;
        node->right = NULL;
        return node;
      
    } else {
      
        struct node* cur;
        
        if(data <= root->data) {
            cur = insert(root->left, data);
            root->left = cur;
        } else {
            cur = insert(root->right, data);
            root->right = cur;
        }
    
        return root;
    }
}
/* you only have to complete the function given below.  
node is defined as  
struct node {
    
    int data;
    struct node *left;
    struct node *right;
  
};
*/
void preOrder(struct node* root) {
    struct node* temp;
    temp = root;
    if (temp != NULL)
    {
        printf("%d ", temp->data);
        preOrder(temp->left);
        preOrder(temp->right);
    }
}
int main() {
  
    struct node* root = NULL;
    
    int t;
    int data;
    scanf("%d", &t);
    while(t-- > 0) {
        scanf("%d", &data);
        root = insert(root, data);
    }
  
    preOrder(root);
    return 0;
}Result

Author And Source
이 문제에 관하여(Hackerrank | Tree: Preorder Traversal), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@2rlo/Hackerrank-Tree-Preorder-Traversal저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)