List the Values Between Low and High
List the Values Between Low and High
Complete collect(Node node, int low, int high, StringBuilder sb) so valuesBetween(low, high) returns every value from low to high, both ends included, smallest first and separated by single spaces. It returns an empty string when no value is in range.
Reminder: a BST lets you skip whole subtrees. In a binary search tree every value in a node's left subtree is smaller than the node, and every value in its right subtree is larger. So a single comparison can prove that an entire side of a node holds nothing you need, and then you never walk into it at all.
50
/ \
30 70
/ \ / \
20 40 60 80
/ \ \
35 45 65Take valuesBetween(35, 60) on this tree. Standing on 30, everything to its left is smaller than 30, which is already below 35, so 20 is never visited. Standing on 60, everything to its right is larger than 60, so 65 is skipped, and 80 is skipped from 70 for the same reason. Visiting in-order (left, node, right) is what keeps the values sorted.
Requirements:
1. Return straight away when node is null; otherwise count the visit with visited++ before anything else.
2. Step into the left subtree only when node.value > low, and into the right subtree only when node.value < high.
3. Between those two steps, add node.value when low <= node.value <= high, with one space before it unless sb is still empty.
4. Do not change valuesBetween or visitedLastTime. The tests print how many nodes your walk visited, so a walk that looks at every node will not pass.
The Node class is provided - do not rename it and do not add public to it, because a Java source file may contain only one public class.
Do NOT write a main method.
Expected Output:
[35 40 45 50 60] 7 [70] 5
= newline. The output ends with a newline — print one after the last line.
Your Output
Run your code to see the output here...