CJCoding With Joseph
15free / daySign inCreate account

Binary Search Tree Inorder Walk and Free

Read an integer n, then n values, and insert each one into a binary search tree: a value smaller than a node goes left, larger goes right. If a value is already in the tree, ignore it — never store the same value twice.

Then print three lines: the inorder traversal, the value at the root, and the height of the tree. Height counts levels, so a tree that is just a root has height 1 and an empty tree has height 0.

For the input:

5
9 4 15 2 7

the program prints exactly the output shown in Expected Output.

Format: Inorder: followed by a space before every value with nothing after the last one, then Root:  and the root's value, then Height:  and the number of levels.

Free the memory you allocate before the program ends — this question checks for memory leaks.

Expected Output:

Inorder: 2 4 7 9 15
Root: 9
Height: 3

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

Hints4 available
Topics:
Pointers
📥 Auto-Input:
5\n9 4 15 2 7\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 (p, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.