标签:style blog http color os width
#include <iostream> #include <string> using namespace std; struct BTNode { int v; // default positive Integer. BTNode *pLeft; BTNode *pRight; BTNode(int x) : v(x), pLeft(NULL), pRight(NULL) {} }; /********************************************************/ /***** Basic functions ***********/ BTNode* createBinaryTree(int r) { BTNode *pRoot = new BTNode(r); int u, v; cin >> u >> v; if(u != 0) pRoot->pLeft = createBinaryTree(u); if(v != 0) pRoot->pRight = createBinaryTree(v); return pRoot; } void release(BTNode *root){ if(root == NULL) return; release(root->pLeft); release(root->pRight); delete[] root; root = NULL; } void print(BTNode *root, int level = 1){ if(root == NULL) { cout << "NULL"; return; }; string s; for(int i = 0; i < level; ++i) s += " "; cout << root->v << endl << s; print(root->pLeft, level+1); cout << endl << s; print(root->pRight, level+1); } /******************************************************************/ void mirrorTree(BTNode *root) { if(!root || (!root->pLeft && !root->pRight)) return; /* 交?换?左ó右ò子ó树÷ */ BTNode *pTem = root->pLeft; root->pLeft = root->pRight; root->pRight = pTem; mirrorTree(root->pLeft); mirrorTree(root->pRight); } int main(){ int TestTime = 3, k = 1; while(k <= TestTime) { cout << "Test " << k++ << ":" << endl; cout << "Create a tree: " << endl; BTNode *pRoot = createBinaryTree(8); print(pRoot); cout << endl; cout << "The mirror tree: " << endl; mirrorTree(pRoot); print(pRoot); cout << endl; release(pRoot); } return 0; }
标签:style blog http color os width
原文地址:http://www.cnblogs.com/liyangguang1988/p/3702933.html