Symmetrical two-fork tree __ Two-forked tree

Source: Internet
Author: User
question: Please implement a function to determine whether a binary tree is symmetrical. If a binary tree is the same as its mirror image, then it is symmetrical.
Detailed design code is as follows:
BOOL Issymmetrical (binarytreenode* pRoot1, binarytreenode* pRoot2);

Determine if the symmetric tree bool Issymmetrical (binarytreenode* proot) {return issymmetrical (Proot, proot);}
        BOOL Issymmetrical (binarytreenode* pRoot1, binarytreenode* pRoot2) {if (pRoot1 = null && PROOT2 = null)
    
    return true;
    
    if (pRoot1 = null | | pRoot2 = NULL) return false;
        
    if (Proot1->m_nvalue!= proot2->m_nvalue) return false; Return issymmetrical (Proot1->m_pleft, Proot2->m_pright) && issymmetrical (Proot1->m_pright, PRoot2
->m_pleft);
}//==================== test Code ==================== void Test (char* testname, binarytreenode* proot, BOOL expected)

    {if (testname!= NULL) printf ("%s begins:", testname);
    if (issymmetrical (proot) = = expected) printf ("passed.\n");
else printf ("failed.\n"); }//8//6 6//5 7 7 5 void Test1 () {binarytreenode* pNode8 = Createbinarytreenode (8);
    binarytreenode* pNode61 = Createbinarytreenode (6);
    binarytreenode* pNode62 = Createbinarytreenode (6);
    binarytreenode* pNode51 = Createbinarytreenode (5);
    binarytreenode* pNode71 = Createbinarytreenode (7);
    binarytreenode* pNode72 = Createbinarytreenode (7);

    binarytreenode* pNode52 = Createbinarytreenode (5);
    Connecttreenodes (PNode8, PNode61, pNode62);
    Connecttreenodes (PNode61, PNode51, pNode71);

    Connecttreenodes (pNode62, pNode72, pNode52);

    Test ("Test1", PNode8, True);
Destroytree (PNODE8); }//8//6 9//5 7 7 5 void Test2 () {binarytreenode* PNode8 = Createbinarytreenode
    (8);
    binarytreenode* pNode61 = Createbinarytreenode (6);
    binarytreenode* PNode9 = Createbinarytreenode (9);
    binarytreenode* pNode51 = Createbinarytreenode (5);
    binarytreenode* pNode71 = Createbinarytreenode (7);
    binarytreenode* pNode72 = Createbinarytreenode (7); binarytreenode* pNode52 = CreaTebinarytreenode (5);
    Connecttreenodes (PNode8, PNode61, PNode9);
    Connecttreenodes (PNode61, PNode51, pNode71);

    Connecttreenodes (PNode9, pNode72, pNode52);

    Test ("Test2", PNode8, false);
Destroytree (PNODE8); //8/6 6//5 7 7 void Test3 () {binarytreenode* pNode8 = Createbinarytreenode (8
    );
    binarytreenode* pNode61 = Createbinarytreenode (6);
    binarytreenode* pNode62 = Createbinarytreenode (6);
    binarytreenode* pNode51 = Createbinarytreenode (5);
    binarytreenode* pNode71 = Createbinarytreenode (7);

    binarytreenode* pNode72 = Createbinarytreenode (7);
    Connecttreenodes (PNode8, PNode61, pNode62);
    Connecttreenodes (PNode61, PNode51, pNode71);

    Connecttreenodes (pNode62, pNode72, NULL);

    Test ("Test3", PNode8, false);
Destroytree (PNODE8);         //5//////3 3/////4 4/// \//2 2///             \//1 1 void Test4 () {binarytreenode* PNODE5 = Createbinarytreenode (5);
    binarytreenode* pNode31 = Createbinarytreenode (3);
    binarytreenode* pNode32 = Createbinarytreenode (3);
    binarytreenode* pNode41 = Createbinarytreenode (4);
    binarytreenode* pNode42 = Createbinarytreenode (4);
    binarytreenode* pNode21 = Createbinarytreenode (2);
    binarytreenode* PNode22 = Createbinarytreenode (2);
    binarytreenode* pNode11 = Createbinarytreenode (1);

    binarytreenode* pNode12 = Createbinarytreenode (1);
    Connecttreenodes (PNODE5, PNode31, pNode32);
    Connecttreenodes (PNode31, pNode41, NULL);
    Connecttreenodes (PNode32, NULL, PNode42);
    Connecttreenodes (pNode41, PNODE21, NULL);
    Connecttreenodes (PNode42, NULL, PNODE22);
    Connecttreenodes (PNode21, PNode11, NULL);

    Connecttreenodes (PNode22, NULL, pNode12);

    Test ("Test4", PNode5, True);
Destroytree (PNODE5);
//5/////3 3/\//4 4/////6 2//////
    1 1 void Test5 () {binarytreenode* PNODE5 = Createbinarytreenode (5);
    binarytreenode* pNode31 = Createbinarytreenode (3);
    binarytreenode* pNode32 = Createbinarytreenode (3);
    binarytreenode* pNode41 = Createbinarytreenode (4);
    binarytreenode* pNode42 = Createbinarytreenode (4);
    binarytreenode* PNode6 = Createbinarytreenode (6);
    binarytreenode* PNode22 = Createbinarytreenode (2);
    binarytreenode* pNode11 = Createbinarytreenode (1);

    binarytreenode* pNode12 = Createbinarytreenode (1);
    Connecttreenodes (PNODE5, PNode31, pNode32);
    Connecttreenodes (PNode31, pNode41, NULL);
    Connecttreenodes (PNode32, NULL, PNode42);
    Connecttreenodes (pNode41, PNode6, NULL);
    Connecttreenodes (PNode42, NULL, PNODE22);
    Connecttreenodes (PNode6, PNode11, NULL);

    Connecttreenodes (PNode22, NULL, pNode12); Test ("Test5", PNoDE5, false);
Destroytree (PNODE5);         //5//////3 3/////4 4///  \//2 2////1 void Test6 () {binarytreenode*
    PNODE5 = Createbinarytreenode (5);
    binarytreenode* pNode31 = Createbinarytreenode (3);
    binarytreenode* pNode32 = Createbinarytreenode (3);
    binarytreenode* pNode41 = Createbinarytreenode (4);
    binarytreenode* pNode42 = Createbinarytreenode (4);
    binarytreenode* pNode21 = Createbinarytreenode (2);
    binarytreenode* PNode22 = Createbinarytreenode (2);

    binarytreenode* pNode12 = Createbinarytreenode (1);
    Connecttreenodes (PNODE5, PNode31, pNode32);
    Connecttreenodes (PNode31, pNode41, NULL);
    Connecttreenodes (PNode32, NULL, PNode42);
    Connecttreenodes (pNode41, PNODE21, NULL);
    Connecttreenodes (PNode42, NULL, PNODE22);
    Connecttreenodes (PNODE21, NULL, NULL); Connecttreenodes (PNode22, NULL, pNode12);

    Test ("Test6", PNode5, false);
Destroytree (PNODE5);
    //Only one node void Test7 () {binarytreenode* pNode1 = Createbinarytreenode (1);

    Test ("Test7", PNode1, True);
Destroytree (PNODE1);

}//No nodes void Test8 () {Test ("Test8", NULL, True);}           All nodes have the same value//5/////5 5///// 5 5/////5 5 void Test9 () {binarytreenode* pNode1 = Createbinaryt
    Reenode (5);
    binarytreenode* pNode21 = Createbinarytreenode (5);
    binarytreenode* PNode22 = Createbinarytreenode (5);
    binarytreenode* pNode31 = Createbinarytreenode (5);
    binarytreenode* pNode32 = Createbinarytreenode (5);
    binarytreenode* pNode41 = Createbinarytreenode (5);

    binarytreenode* PNode42 = Createbinarytreenode (5);
    Connecttreenodes (PNode1, PNODE21, PNODE22);
    Connecttreenodes (PNode21, PNode31, NULL); Connecttreenodes (PNode22, NULL, pNode32);
    Connecttreenodes (PNode31, pNode41, NULL);
    Connecttreenodes (PNode32, NULL, PNode42);
    Connecttreenodes (pNode41, NULL, NULL);

    Connecttreenodes (pNode42, NULL, NULL);

    Test ("Test9", PNode1, True);
Destroytree (PNODE1);           //all nodes have the same value//5//////5 5///// 5 5/////5 5 void Test10 () {binarytreenode* pNode1 = Createbinarytre
    Enode (5);
    binarytreenode* pNode21 = Createbinarytreenode (5);
    binarytreenode* PNode22 = Createbinarytreenode (5);
    binarytreenode* pNode31 = Createbinarytreenode (5);
    binarytreenode* pNode32 = Createbinarytreenode (5);
    binarytreenode* pNode41 = Createbinarytreenode (5);

    binarytreenode* PNode42 = Createbinarytreenode (5);
    Connecttreenodes (PNode1, PNODE21, PNODE22);
    Connecttreenodes (PNode21, PNode31, NULL);
    Connecttreenodes (PNode22, NULL, pNode32); ConnecttReenodes (PNode31, pNode41, NULL);
    Connecttreenodes (PNode32, PNode42, NULL);
    Connecttreenodes (pNode41, NULL, NULL);

    Connecttreenodes (pNode42, NULL, NULL);

    Test ("Test10", PNode1, false);
Destroytree (PNODE1);
    } void Main (int argc, char* argv[]) {Test1 ();
    Test2 ();
    Test3 ();
    Test4 ();
    Test5 ();
    Test6 ();
    Test7 ();
    Test8 ();
    Test9 ();
Test10 (); }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.