读书笔记: 复制树 Copy Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
typedef struct tree_node *tree_pointer;
typedef struct tree_node{
    char *data;
    tree_pointer left_child,right_child;
}tree_node;

tree_pointer copy(tree_pointer original){
    tree_pointer temp;
    if(original){
        temp = (tree_pointer) malloc (sizeof(tree_node));
        if(!temp){
            exit(1);
        }
        temp->data = original->data;
        temp->left_child = copy(original->left_child);
        temp->right_child = copy(original->right_child);
        return temp;
    }
    return NULL;
}

int equal(tree_pointer first,tree_pointer second){
    return ( (!first && !second) ||
             (first && second && first->data == second->data) &&
             equal(first->left_child, second->left_child) &&
             equal(first->right_child, second->right_child)
            );
}