Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
inorder traversal
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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); }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.37 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.52 sec
fork mode
|
history
|
discussion
4 2 5 1 8 6 9 3 7 4 2 5 1 8 6 9 3 7