Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Make Bin Tree
//Title of this code #include <iostream> #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) {} }; int depth(BinTree* root, int d) { if (!root) return d; return max(depth(root->left, d + 1), depth(root->right, d + 1)); } int maxDepth(int nodeCount) { int i = 0; while(nodeCount > 0) { nodeCount >>= 1; ++i; } return i; } void inorder(BinTree* root, vector<BinTree*>& t) { if (!root) return; inorder(root->left, t); cout << root->data << " "; t.push_back(root); inorder(root->right, t); } void BuildNew(vector<BinTree*>& t, BinTree*& root, int begin, int end) { //return; if (begin > end) return; int mid; if (begin == end) mid = begin; else mid = (end - begin) / 2 + begin; root = t[mid]; //cout << root->data << " "; BuildNew(t, root->left, begin, mid - 1); BuildNew(t, root->right, mid + 1, end); } void print(vector<BinTree*>& t) { for (int i = 0; i < t.size(); ++i){ cout << t[i]->data << " "; t[i]->left = NULL; t[i]->right = NULL; } cout << endl; } int main() { BinTree *ggg = new BinTree(19, NULL, NULL); BinTree *gg = new BinTree(18, NULL, ggg); BinTree *ddd = new BinTree(1, NULL, NULL); BinTree *dd = new BinTree(2, ddd, NULL); BinTree *hh = new BinTree(13, NULL, NULL); BinTree *j = new BinTree(10, NULL, NULL); BinTree *h = new BinTree(14, hh, NULL); BinTree *g = new BinTree(17, NULL, gg); BinTree *f = new BinTree(12, j, h); BinTree *e = new BinTree(7, NULL, NULL); BinTree *d = new BinTree(3, dd, NULL); BinTree *c = new BinTree(15, f, g); BinTree *b = new BinTree(5, d, e); BinTree *root = new BinTree(9, b, c); vector<BinTree*> t; inorder(root, t); cout << endl; cout << depth(root, 0); cout << endl; print(t); cout << endl; BinTree *r = t[t.size() / 2]; BuildNew(t, r, 0, t.size() - 1); inorder(r, t); //cout << maxDepth(13); }
run
|
edit
|
history
|
help
0
mur1
СПКИ АП КЭП 3
template
maximum_frequent_sum
GET ALL PRIME FACTORS OF A NUMBER
Camel case
tes
basic caculate iii
Using C's sprinf() Function to Format Output String
Kth Smallest Element (with extra space)