Question: enter a binary search tree and convert the tree to its image. That is, in the converted Binary Search Tree, the left subtree has more nodes than the right subtree. Use recursive and cyclic methods to convert tree images. For example, enter:
8
/\
6 10
/\/\
5 7 9 11
Output:
8
/\
10 6
/\/\
11 9 7 5
This question is relatively simple. There is nothing to say.Code...
Void bstree: effecrec (bstreenode * node) {If (! Node) return; between REC (node-> Lc); between REC (node-> RC); bstreenode * temp = node-> lC; // switch left and right subtree node> lc = node> RC; node> rc = temp;} void bstree: javasrec () // recursion {dead Rec (Root);} void bstree: Mirror () // non-recursion {stack <bstreenode *> st; ST. push (Root); // press the root node of the binary tree to stack while (! St. empty () {bstreenode * t = ST. top (); bstreenode * temp; ST. pop (); temp = T-> lC; // switch left and right subtree T-> lc = T-> RC; t-> rc = temp; if (t-> Lc) ST. push (t-> Lc); If (t-> RC) ST. push (t-> RC );}}