首页 > 代码库 > 博客测试
博客测试
#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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。