| preferred AP College board partner for AP classes
AP Computer Science A/Unit 10: Recursion
Start Practice TestPractice Test
About Exam
easy Solved by 2 students
Recursive Pre-Order Tree Traversal
< Prev
Next >

What is the order of nodes printed during the pre-order traversal of the tree?

class Node {
    String val;
    Node left, right;
    Node(String v) { val = v; }
}

public class PreorderTraversal {
    public static void preOrder(Node root) {
        if(root == null) return;
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    
    public static void main(String[] args) {
        Node a = new Node("A");
        Node b = new Node("B");
        Node c = new Node("C");
        Node d = new Node("D");
        a.left = b;
        a.right = c;
        b.left = d;
        preOrder(a);
    }
}
A

A D B C

B

C A B D

C

D B A C

D

A B D C

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: [email protected]|Privacy Policy