Recursive Pre-Order Tree Traversal
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
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE