Inverted mirror of binary tree __ two fork Tree Flip

Source: Internet
Author: User

The mirror of the binary tree is a two-forked tree with a symmetrical binary tree, such as

After mirroring

is to exchange the left subtree and right subtree pointers for each non-leaf node
1: Recursion, if the node is empty, return, otherwise swap left and right children pointer, recursive mirror node of the left subtree, the right-hand subtree;
2: Non-recursive: Swap the left subtree and right subtree pointers for each non-leaf node, use the queue, the root node first to the team, swap the first node of the child's Needle, then the first node of the left and the child team, then pop (), until the queue is empty, that is, traversal completed;

Code as follows, the deficiencies of a lot of excuse, a lot of advice;

#include <iostream> #include <stack> #include <queue> using namespace std;
    typedef struct NODE {char data;
    Node*lchild;
Node*rchild;

}node;
    Create a node Node*buynode () {node* p = new (nothrow) nodes;
    if (p==null) {exit (-1);
    } memset (P,0,sizeof (Node));
return p;
    //Create a binary tree node* createtree (char *&s) {if (s==null) {return NULL;
    } node*p=null;
        if ((*s)!= ' $ ') {p = Buynode ();
        P->data = *s;
        P->lchild = Createtree (++s);
    P->rchild = Createtree (++s);
} return p;
        }//First-order recursive traversal void frond (node*p) {if (p) {cout<<p->data<< ';
        Frond (P->lchild);
    Frond (P->rchild);
    }//First-order non-recursive traversal void _frond (node*p) {stack<node*>s;
            while (P!=null | |!s.empty ()) {while (p!=null) {s.push (P);
            cout<<p->data<< "";

        p=p->lchild;
  }      if (!s.empty ()) {p= s.top ();
            S.pop ();
        p=p->rchild;
    }}//Hierarchy traversal void Cenci (node*p) {if (p==null) {return;
    int m = 0;
    queue<node*>s;
    int qsize;
    S.push (P);        
        while (!s.empty ()) {m++;
        Qsize = S.size ();//record the number of nodes in the current level int i=0;
            while (i<qsize) {P=s.front ();
            if (p->lchild) {s.push (p->lchild);
            } if (P->rchild) {s.push (p->rchild);
            } cout<<p->data<< "";
            S.pop ();
        i++; cout<< "Current level:" <<m<<endl;//display current Hierarchy}//non-recursive flip void _change (node*p) {if p==
    NULL) {return;
    } queue<node*>s;     S.push (P);
      First join the root node while (!s.empty ()) {node*t = S.front ();  Node*tep = t->lchild;
        T->lchild = t->rchild;
        T->rchild = Tep;
        if (t->lchild!=null)//into the left child {s.push (t->lchild);
        } if (T->rchild!=null)//into the right child {s.push (t->rchild); } s.pop ();
    Out team}///recursive flip void Change (node*p) {if (p==null) {return;
    Swap (P->lchild, p->rchild);
    Change (P->lchild);
Change (P->rchild);
    int main () {char*s = "124$$57$$$36$8$$$";
    Node*root=createtree (s);
    cout<< "First order traversal:";
    Frond (root);
    cout<<endl;
    cout<< "First order traversal:";
    _frond (root);
    cout<<endl;
    cout<< "Hierarchy traversal:" <<endl;
    Cenci (root);
    cout<<endl;
    _change (root);
    cout<<endl;
    Cenci (root);
    cout<< "First order traversal:";
    Frond (root);
return 0; }

Run Result:
First-order Traversal: 1 2 4 5 7 3 6 8
First-order Traversal: 1 2 4 5 7 3 6 8
Hierarchy traversal:
1 Current level: 1
2 3 Current level: 2
4 5 6 Current level: 3
7 8 Current level: 4

1 Current level: 1
3 2 Current level: 2
6 5 4 Current level: 3
8 7 Current level: 4
First-order Traversal: 1 3 6 8 2 5 7 4 Please press any key to continue ...

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.