Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Mirror a binary tree
#include <iostream> #include <list> #include <vector> using namespace std; struct BinTree { int data; BinTree *left; BinTree *right; BinTree(int d, BinTree* l, BinTree* r): data(d), left(l), right(r) {} }; void mirrorTree(BinTree* root) { if (!root) return; mirrorTree(root->left); mirrorTree(root->right); swap(root->left, root->right); } void printInorder(BinTree* root) { if (!root) return; printInorder(root->left); cout << root->data << " "; printInorder(root->right); } // 4 // / \ // 2 8 // / \ / \ // 1 3 6 9 // / \ // 5 7 int main() { BinTree *h = new BinTree(7, NULL, NULL); BinTree *j = new BinTree(5, NULL, NULL); BinTree *g = new BinTree(9, NULL, NULL); BinTree *f = new BinTree(6, j, h); BinTree *e = new BinTree(3, NULL, NULL); BinTree *d = new BinTree(1, NULL, NULL); BinTree *c = new BinTree(8, f, g); BinTree *b = new BinTree(2, d, e); BinTree *a = new BinTree(4, b, c); printInorder(a); cout << endl; mirrorTree(a); printInorder(a); cout << endl; }
run
|
edit
|
history
|
help
0
4149 coj TL
2
Średnia bez zera
Prime_Number_Cpp
TempQuickDoubArray2
BinTree playground
ONP without brackets
StackQuiz
SEGMENTED SIEVE
mine