• 4-11 Isomorphic (10分)


    Two trees, T1 and T2, are isomorphic if T1 can be transformed into T2 by swapping left and right children of (some of the) nodes in T1. For instance, the two trees in Figure 1 are isomorphic because they are the same if the children of A, B, and G, but not the other nodes, are swapped. Give a polynomial time algorithm to decide if two trees are isomorphic.

    Format of functions:

    int Isomorphic( Tree T1, Tree T2 );

    where Tree is defined as the following:

    1. typedef struct TreeNode *Tree;
    2. struct TreeNode { ElementType Element; Tree Left; Tree Right; };

    The function is supposed to return 1 if T1 and T2 are indeed isomorphic, or 0 if not.

    Sample program of judge:

    1. #include
    2. #include
    3. typedef char ElementType;
    4. typedef struct TreeNode *Tree;
    5. struct TreeNode {
    6. ElementType Element;
    7. Tree Left;
    8. Tree Right;
    9. };
    10. Tree BuildTree(); /* details omitted */
    11. int Isomorphic( Tree T1, Tree T2 );
    12. int main()
    13. { Tree T1, T2;
    14. T1 = BuildTree();
    15. T2 = BuildTree();
    16. printf(“%d\n”, Isomorphic(T1, T2));
    17. return 0; }
    18. /* Your function will be put here */

    Sample Output 1 (for the trees shown in Figure 1):

    1
    

    Sample Output 2 (for the trees shown in Figure 2):

    0

     

    伪代码:

    1. int Isomorphic(Tree R1,Tree R2)
    2. {
    3. if(都是空树) return 1 ; //同构
    4. if(一个树为空树,一个不是空树) return 0 ; //不同构
    5. if(树根的值本身不相同) return 0 ;
    6. if(左子树都为空)/*不需要swap*/
    7. return Isomorphic(R1->Right,R2->Right);//只需要向下判断右子树
    8. /*能执行到这里说明左子树不同时为空*/
    9. if( 左子树同时不为空 && 左子树的值相同 )/*此时不需要swap*/
    10. return Isomorphic(R1->Right,R2->Right)&& Isomorphic(R1->Lift,R2->Lift);/*判断左左、右右子树是否同构*/
    11. /*执行到这里,有两种可能:左子树一个为空一个不为空 或 左子树同时不为空但是他们元素的值不同,这时就需要swap了*/
    12. return Isomorphic(R1->Lift,R2->Right)&&Isomorphic(R1->Right,R2->Lift);

    代码:

    1. int Isomorphic( Tree T1, Tree T2 )
    2. {
    3. if(!T1&&!T2)return 1;//同时为空树
    4. if((T1&&!T2)||(!T1&&T2))return 0;//一个是空树,一个不是空树
    5. if(T1->Element!=T2->Element)return 0;//树根的值不一样
    6. if(!T1->Left&&!T2->Left)return Isomorphic(T1->Right,T2->Right);//左树同时为空,则判断右树
    7. else if(T1->Left&&!T2->Left)return Isomorphic(T2->Right,T1->Left);//左树一个为空一个不为空
    8. else if((!T1->Left&&T2->Left))return Isomorphic(T1->Right,T2->Left);
    9. //左树同时不为空
    10. if(T1->Left->Element==T1->Left->Element)
    11. return Isomorphic(T1->Left,T2->Left)&&Isomorphic(T1->Right,T2->Right);
    12. return Isomorphic(T1->Left,T2->Right)&&Isomorphic(T1->Right,T2->Left);
    13. }

    评价: 

    问题看着挺难,但是把问题想清楚就容易很多了。

  • 相关阅读:
    Java文件操作
    哈夫曼树构建、编码、译码C++实现
    Git 常用设置|别名、邮箱、免密
    Gradle笔记 七 publishing 项目发布
    C# range
    科技碰撞:元宇宙与虚幻引擎,被掩盖的底层逻辑何在?
    基于SSM+Vue的校园共享单车管理系统
    一行代码修复100vh bug | 京东云技术团队
    什么是机器学习中的目标函数和优化算法,列举几种常见的优化算法
    【概率论与数理统计(研究生课程)】知识点总结8(假设检验)
  • 原文地址:https://blog.csdn.net/Zhonexixi/article/details/127595207