Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
inorder traversal
//Title of this code #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 inorder_recursive(BinTree *root) { if (root != NULL) { inorder_recursive(root->left); cout << root->data << " "; inorder_recursive(root->right); } } void inorderTraversal(BinTree *root) { list<BinTree*> stack; BinTree *cur = root; while (!stack.empty() || cur != NULL) { if (cur) { stack.push_back(cur); cur = cur->left; } else { cur = stack.back(); stack.pop_back(); cout << cur->data << " "; cur = cur->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); inorderTraversal(a); cout << endl; inorder_recursive(a); }
run
|
edit
|
history
|
help
0
krack
process memory structure
pow implementation
error
LRU cache
CV
ShoppingList
max_size()_30-Seconds-of-C++
Working variables benchmark
Sangharsh.vhawale