Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
BintTree vertical sum
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> #include <queue> #include <algorithm> #include <map> 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 verticalSum(map<int,int>& m, BinTree* root, int d) { if (!root) return; if (m.find(d) == m.end()) m[d] = 1; else ++m[d]; verticalSum(m, root->left, d - 1); verticalSum(m, root->right, d + 1); } void printVerticalSum(BinTree* root) { map<int,int> m; verticalSum(m, root, 0); for (auto it = m.begin(); it != m.end(); ++it) { cout << it->first << " - " << it->second << endl; } } int main() { BinTree *j = new BinTree(10, NULL, NULL); BinTree *h = new BinTree(14, NULL, NULL); BinTree *g = new BinTree(17, NULL, NULL); BinTree *f = new BinTree(12, j, h); BinTree *e = new BinTree(7, NULL, NULL); BinTree *d = new BinTree(3, NULL, NULL); BinTree *c = new BinTree(15, f, g); BinTree *b = new BinTree(5, d, e); BinTree *root = new BinTree(9, b, c); printVerticalSum(root); }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.69 sec, absolute running time: 0.04 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.74 sec
fork mode
|
history
|
discussion
-2 - 1 -1 - 2 0 - 3 1 - 2 2 - 1