Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Blog
BST to DLL
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
#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 BSTtoDLL(BinTree* node, BinTree*& head, BinTree*& tail){ if(!node) return; /* Go to left most child */ if(node->left) BSTtoDLL(node->left, head, tail); /* If this wasn't the first node being added to list*/ if(tail != NULL) tail->right = node; else head = node; /* make left pointer point to last node, and update the last node to current*/ node->left = tail; tail = node; /* If there is right child, process right child */ if(node->right) BSTtoDLL(node->right, head, tail); } void printList(BinTree *root) { while (root) { cout << root->data << " "; root = root->right; } cout << endl; } // 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); BinTree *head = NULL; BinTree *tail = NULL; BSTtoDLL(a, head, tail); printList(head); }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.35 sec, absolute running time: 0.04 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.4 sec
edit mode
|
history
|
discussion
1 2 3 4 5 6 7 8 9