Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Nth max element in BST
#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) {} }; static int nn = 4; void inorderTraversal2(BinTree *root) { if (!root || nn == 0) return; inorderTraversal2(root->right); if (nn > 0) { cout << root->data << " "; --nn; } inorderTraversal2(root->left); } void inorderTraversal(BinTree *root, int n) { list<BinTree*> stack; BinTree *cur = root; while ((!stack.empty() || cur != NULL) && n > 0) { if (cur) { stack.push_back(cur); cur = cur->right; } else { cur = stack.back(); stack.pop_back(); cout << cur->data << " "; cur = cur->left; --n; } } } // 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); inorderTraversal2(a); cout << endl; inorderTraversal(a, 4); }
run
|
edit
|
history
|
help
0
1st
combine c++ string with dynamically allocated c array of chars through overloaded add operator
Test 5(2020)
random lotto number game
read_write_lock_acc
Find the max and min number in array
Do While Meteoro Agustin
Shorting in one line using class members std and boost bind
Continuous Sub Set with given sum
barai_1