Advanced Topics MCQ
Code Tracing: Trees
Code Tracing: Trees
Question 1 of 25
Answered 0 of 25
Choose one answer.
What is printed by the recursive tree method?
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));
}
}