Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Iteration
Start Practice TestPractice Test
About Exam
hard

Iterative In-Order Tree Traversal

Mark
< Prev
Next >

What is the output when the main method of the BSTTest class is executed?

public class BSTTest {
    static class Node {
        int val;
        Node left, right;
        Node(int val) { this.val = val; }
    }
    public static void main(String[] args) {
        Node root = new Node(10);
        root.left = new Node(5);
        root.right = new Node(15);
        root.left.left = new Node(2);
        root.left.right = new Node(7);
        java.util.Stack<Node> stack = new java.util.Stack<>();
        Node curr = root;
        StringBuilder sb = new StringBuilder();
        while(curr != null || !stack.isEmpty()) {
            while(curr != null) {
                stack.push(curr);
                curr = curr.left;
            }
            curr = stack.pop();
            sb.append(curr.val).append(" ");
            curr = curr.right;
        }
        System.out.println(sb.toString().trim());
    }
}
A

15 10 7 5 2

B

10 5 2 7 15

C

2 5 10 7 15

D

2 5 7 10 15

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|