Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Bin Tree build
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 <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) {} }; int depth(BinTree* root, int d) { if (!root) return d; return max(depth(root->left, d + 1), depth(root->right, d + 1)); } int maxDepth(int nodeCount) { int i = 0; while(nodeCount > 0) { nodeCount >>= 1; ++i; } return i; } void inorder(BinTree* root) { if (!root) return; inorder(root->left); cout << root->data << " "; inorder(root->right); } void resetTree(BinTree* root, vector<BinTree*>& t) { if (!root) return; resetTree(root->left, t); t.push_back(root); resetTree(root->right, t); root->left = NULL; root->right = NULL; } void BuildNew(vector<BinTree*>& t, BinTree*& root, int begin, int end) { if (begin > end) return; int mid; if (begin == end) mid = begin; else mid = (end - begin) / 2 + begin; root = t[mid]; BuildNew(t, root->left, begin, mid - 1); BuildNew(t, root->right, mid + 1, end); } void print(vector<BinTree*>& t) { for (int i = 0; i < t.size(); ++i){ cout << t[i]->data << " "; } cout << endl; } int main() { BinTree *ggg = new BinTree(19, NULL, NULL); BinTree *gg = new BinTree(18, NULL, ggg); BinTree *ddd = new BinTree(1, NULL, NULL); BinTree *dd = new BinTree(2, ddd, NULL); BinTree *hh = new BinTree(13, NULL, NULL); BinTree *j = new BinTree(10, NULL, NULL); BinTree *h = new BinTree(14, hh, NULL); BinTree *g = new BinTree(17, NULL, gg); BinTree *f = new BinTree(12, j, h); BinTree *e = new BinTree(7, NULL, NULL); BinTree *d = new BinTree(3, dd, NULL); BinTree *c = new BinTree(15, f, g); BinTree *b = new BinTree(5, d, e); BinTree *root = new BinTree(9, b, c); vector<BinTree*> t; inorder(root); cout << endl; cout << depth(root, 0); cout << endl; resetTree(root, t); root = t[t.size() / 2]; BuildNew(t, root, 0, t.size() - 1); inorder(root); cout << endl; cout << depth(root, 0); cout << endl; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.45 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.64 sec
edit mode
|
history
|
discussion
1 2 3 5 7 9 10 12 13 14 15 17 18 19 5 1 2 3 5 7 9 10 12 13 14 15 17 18 19 4