| 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
< 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

2 5 7 10 15

C

2 5 10 7 15

D

10 5 2 7 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.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

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