1, Binary tree definition:
typedef struct BTREENODEELEMENT_T_ { void *data;} btreenodeelement_t;typedef struct Btreenode_t_ { btreenodeelement_t *m_pelemt; struct Btreenode_t_ *m_pleft; struct btreenode_t_ *m_pright;} btreenode_t;
2. Find binary Tree image
For example:
A A
b c ====> c b
D e E D
(1) Recursive method
If Proot is null, it is an empty tree and returns;
If the proot is not NULL, the proot of the left and right nodes are exchanged, and then the image of the left and right subtree is respectively obtained;
void Btreemirror (btreenode_t *proot) {
if (Proot = = NULL)
return;
btreenode_t *ptemp = proot->m_pleft;
Proot->m_pleft = proot->m_pright;
Proot->m_pleft = ptemp;
Btreemirror (Proot->m_pleft);
Btreemirror (Proot->m_pright);
Return
}
(2) Non-recursive mode
Step Description: With queue
First, proot the root node into the queue;
The first step: when the queue is not empty, gets the total number of nodes at the current level, that is, the length of the current queue; perform the second step;
The second step: according to the total number of nodes in the current layer, the team to traverse the node, in the traversal, the exchange of left and right nodes, if the left and right nodes exist, then the queue; when traversing all nodes in the current layer, traverse the next layer, performing the first step.
void Btreemirror (btreenode_t *proot) { if (proot = = null) return null; Queue <btreenode_t *> que; Que.push (proot); int curlevelnodestotal = 0; while (!que.empty ()) { curlevelnodestotal = Que.size (); int cnt = 0; while (CNT < curlevelnodestotal) { ++cnt; Proot = Que.front (); Que.pop (); btreenode_t *ptemp = proot->m_pleft: proot->m_pleft = proot->m_pright; Proot->m_pright = ptemp; if (proot->m_pleft! = NULL) que.push (proot->m_pleft); if (proot->m_pright! = NULL) que.push (proot->m_pright);} } return;}
Binary tree (one)----to find the image of binary tree, recursive and non-recursive way