Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
generate BinTree from sorted list
//Title of this code #include <algorithm> #include <iostream> #include <vector> #include <queue> using namespace std; struct Node { int data; Node* left; Node* right; Node(int d): data(d), left(NULL), right(NULL) {} Node(int d, Node* l, Node* r): data(d), left(l), right(r) {} }; Node* generateBinTree(vector<int>& t, int begin, int end) { if (begin > end) return NULL; int mid; if (begin == end) mid = begin; else mid = ((end - begin) / 2) + begin; return new Node(t[mid], generateBinTree(t, begin, mid - 1), generateBinTree(t, mid + 1, end)); } void inorder(Node* root) { if (!root) return; inorder(root->left); cout << root->data << " "; inorder(root->right); } int main() { vector<int> t; t.push_back(1); t.push_back(2); t.push_back(4); t.push_back(3); t.push_back(7); t.push_back(6); t.push_back(5); t.push_back(8); t.push_back(9); sort(t.begin(), t.end()); Node *root = generateBinTree(t, 0, t.size() - 1); inorder(root); }
run
|
edit
|
history
|
help
0
Simple use of function templete and namespace
on_off
cppPyProperty
Rectangle overlap
Translating binary to decimal numbers
Discounting
NameTempSpecial2
combine c++ string with dynamically allocated c array of chars through overloaded add operator
Let's Go!
12hours Clock c++