#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int value;
    struct node *leftChild;
    struct node *rightChild;
}Tree;

Tree *copyBT(Tree *root)
{
    if(!root) return NULL;
    
    Tree *TreeNode = (Tree *)malloc(sizeof(Tree));
    TreeNode->value = root->value;
    TreeNode->leftChild  = copyBT(root->leftChild);
    TreeNode->rightChild = copyBT(root->rightChild);

    return TreeNode; 
}

int equalBT(Tree *root1, Tree *root2)
{
    if(!root1 && !root2)
        return 1;

    if( (!root1 && root2) || (root1 && !root2))
        return 0;

    //printf("%d %d\n", root1->value, root2->value);  //for you to verify
    return (root1->value == root2->value) && equalBT(root1->leftChild, root2->leftChild)
           && equalBT(root1->rightChild, root2->rightChild);
}

// just create a tree without any skill
void createTree(Tree *root)
{
    /*   1
       2    3
     4   5   6 */

    Tree *node[5];
    for(int i = 0; i<5; i++){
        node[i] = (Tree *)malloc(sizeof(Tree *));
        node[i]->value = i+2;
    }

    root->value = 1;
    root->leftChild = node[0];
    root->rightChild = node[1];
    node[0]->leftChild = node[2];
    node[0]->rightChild = node[3];
    node[1]->rightChild = node[4];
}


int main()
{
    Tree *Tree_A, *Tree_B;
    Tree_A = (Tree *)malloc(sizeof(Tree));
    createTree(Tree_A);
    Tree_B = copyBT(Tree_A);

    if( equalBT(Tree_A, Tree_B) )
        printf("complete!\n");
    else
        printf("error!\n");

    return 0;
}

arrow
arrow
    全站熱搜

    大神(偽) 發表在 痞客邦 留言(0) 人氣()