← Back to Pointers
Question 474
Binary Search Tree Inorder Walk and Free
Binary Search Tree Inorder Walk and Free
Read an integern, thennvalues, 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, thenRoot:and the root's value, thenHeight: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
Sign in to use hintsTopics:
Pointers
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.