Binary Search Tree In-Order Traversal
What is the output of the inOrder traversal when the program is executed?
public class TreeTest {
static class Node {
int data;
Node left, right;
Node(int value) { data = value; }
}
public static Node insert(Node root, int value) {
if(root == null) {
return new Node(value);
}
if(value < root.data) {
root.left = insert(root.left, value);
} else if(value > root.data) {
root.right = insert(root.right, value);
}
return root;
}
public static void inOrder(Node root) {
if(root == null) return;
inOrder(root.left);
System.out.print(root.data + " ");
inOrder(root.right);
}
public static void main(String[] args) {
Node root = null;
int[] vals = {10, 5, 15, 12, 18};
for(int v : vals) {
root = insert(root, v);
}
inOrder(root);
}
}
A
5 10 12 15 18
B
5 10 15 12 18
C
10 5 15 12 18
D
10 5 12 15 18
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE