Advanced Topics MCQ
Code Tracing: Trees
Practice mode shows one question at a time and lets students check each answer before moving on.
For practice use only.
Code Tracing: Trees
Code Tracing: Trees with immediate answer checks and explanations for Advanced Topics in Computer Science.
Question 1 of 25
Answered 0 of 25
Choose one answer.
Trace the recursive tree method. What is printed?
class Node { int value; Node left, right; Node(int v) { value = v; } }
public class WeightedTree
{
public static int score(Node n, int depth)
{
if (n == null) return 0;
return n.value * (depth + 1) + score(n.left, depth + 1) - score(n.right, depth + 1);
}
public static void main(String[] args)
{
Node root = new Node(8);
root.left = new Node(4);
root.right = new Node(12);
root.left.left = new Node(2);
root.left.right = new Node(6);
root.right.right = new Node(15);
System.out.println(score(root, 0));
}
}