• 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 简洁初始化类】匿名内部类和实例初始化块
    [LeetCode/力扣][Java] 0315. 计算右侧小于当前元素的个数(Count of Smaller Numbers After Self)
    企业直播怎么从公域引流
    java连接数据库SQL注入问题的解决
    【C++】pow函数实现的伽马变换详解和示例
    基于java+springboot+mybatis+vue+elementui的人职匹配推荐系统
    刷题学习记录(攻防世界)
    和想象中完全不一样?巡课在线就能进行
    Spring boot装载模板代码工程实践问题
    【计算机毕业设计】基于SpringBoot的电影在线预定与管理系统的设计与实现
  • 原文地址:https://blog.csdn.net/Zhonexixi/article/details/127595207