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) { vector<BinTree*> stack; BinTree *cur = root; while (true) { if (cur) { stack.push_back(cur); cur = cur->left; } else { if (!stack.empty()) { cur = stack.back(); stack.pop_back(); cout << cur->data << " "; //doSomething(cur->data); cur = cur->right; } else { break; } } } } int main() { BinTree *j = new BinTree(9, NULL, NULL); BinTree *h = new BinTree(8, NULL, NULL); BinTree *g = new BinTree(7, NULL, NULL); BinTree *f = new BinTree(6, h, j); BinTree *e = new BinTree(5, NULL, NULL); BinTree *d = new BinTree(4, NULL, NULL); BinTree *c = new BinTree(3, f, g); BinTree *b = new BinTree(2, d, e); BinTree *a = new BinTree(1, b, c); inorderTraversal(a); cout << endl; inorder_recursive(a); }
run
|
edit
|
history
|
help
0
list
Click 5
stlsizeof.cc
How to get base class
Network UVa
ProPriceTemp
MyString
Float
D. Traveling Graph
A+B ორობით სისტემაში