Mirror: The right tree is created from the left tree, as seen from the mirror.
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7F/8A/wKioL1ch_xzTdUzAAAAURc2HQwA925.png "title=" QQ picture 20160428201042.png "alt=" Wkiol1ch_xztduzaaaaurc2hqwa925.png "/>
#include <iostream> #include <stack>using namespace std;struct binarytreenode{int data; binarytreenode* left; binarytreenode* right; Binarytreenode (int x):d ata (x), left (null), right (null) {}};class binarytree{protected: binarytreenode* _root; Binarytreenode* _createbinarytree (int* arr, int& index, int size) { binarytreenode* root = null;if (index<size&&arr[index] != ' # ') {root = new binarytreenode (Arr[index]); Root->left = _createbinarytree (Arr, ++index , size); Root->right = _createbinarytree (arr, ++index, size);} Return root;} Binarytreenode* _copymirrorint (Binarytreenode* root) {if (root == null) return NULL; Binarytreenode* tmp = new binarytreenode (root->data); tmp->left = _ Copymirrorint (root->right); Tmp->right= _copymirroRint (root->left); return tmp;} Public:binarytree (): _root (NULL) {}binarytree (int *arr, int size) {int index = 0;_ Root = _createbinarytree (arr, index, size);} Void preorder_non () {if (_root == null) return; Binarytreenode* cur = _root;stack<binarytreenode*> s;s.push (_root);while (! S.empty ()) {cur= s.top ();p rintf ("%d ", cur->data); S.pop ();if (cur->right) S.push (cur- >right);if (cur->left) s.push (cur->left);} Cout << endl;} Void inorder_non () {if (_root == null) return;stack<binarytreenode*> s; binarytreenode* cur = _root;while (cur | | !s.empty ()) {while (cur) {s.push (cur); cur = cur->left;} if (!s.empty ()) {cout << s.top ()->data << " ";cur = S.top ()->right;s.pop ();}} Cout << endl;} Void postordeR_non () {if (_root == null) return;stack<binarytreenode*> s; binarytreenode* cur = _root; binarytreenode* prev = null; binarytreenode* top = null;while (cur | | !s.empty ()) {while (cur) {s.push (cur); cur = cur->left;} Top = s.top ();if (top->right == null | | top->right == prev) {cout << top->data << " "; S.pop ( );p Rev = top;} Else{cur = top->right;}} Cout << endl;} Void copymirroring (binarytree& b) {_root = _copymirrorint (b._root);}}; Void test1 () {int arr[] = { 1, 2, 4, ' # ', ' # ', 5, ' # ', ' # ', 3, 6, ' # ', ' # ', 7 }; BINARYTREE&NBSP;BT1 (arr, sizeof (arr) / sizeof (arr[0])); Binarytree bt2;bt1. Inorder_non (); bt1. Postorder_non (); bt2. Copymirroring (bT1); Bt2. Postorder_non ();}
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1768768
Image of a binary tree