CJCoding With Joseph
15free / daySign inCreate account

Binary Search Tree with Recursive Teardown

Build a binary search tree out of heap-allocated nodes, print it in order, then tear the whole thing down.

Input (stdin):
- Line 1: an integer n, how many numbers follow (n is always at least 1)
- Line 2: n integers separated by spaces

Insert each number into a binary search tree in which every node is created with new. Smaller values go left, larger values go right, and a value that is already in the tree is IGNORED (no duplicate node is created).

Then print exactly two lines. For the input 7 then 50 30 70 20 40 60 80 the output is exactly what is shown in Expected Output.

Line 1 is the literal text Inorder: followed by every stored value in ascending order, each value preceded by a single space. Line 2 is the literal text Count:  followed by how many nodes you actually created.

Free the memory you allocate - this question checks for memory leaks.

Write everything yourself, including main.

Expected Output:

Inorder: 20 30 40 50 60 70 80
Count: 7

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Pointers
📥 Auto-Input:
7\n50 30 70 20 40 60 80\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.