Preorder Traversal is a variation of Tree DFS where the root is processed BEFORE its children: Root (Self) -> Left Child -> Right Child.
Because it processes the parent node immediately upon discovery, Preorder is used whenever you need to aggressively explore or duplicate a tree structure.
If you traverse a tree using Preorder and insert those exact values sequentially into a new, blank tree, you will perfectly clone the original structure. If you try doing that with an Inorder traversal, you will end up creating a degenerate, unbalanced Linked List instead of a clone.
public class Preorder {
static class TreeNode {
int val; TreeNode left, right;
}
public void preorder(TreeNode root) {
if (root == null) return;
// 1. Process the Root (Self) FIRST
System.out.print(root.val + " ");
// 2. Go down the Left side
preorder(root.left);
// 3. Go down the Right side
preorder(root.right);
}
}What is Preorder Traversal primarily useful for?
Cloning a tree structure. By visiting the root first, you can instantiate the parent node in the clone tree before you attempt to attach the child nodes to it.
Cloning an exact binary tree structure into a new object in memory. You cannot attach a left child to a parent if the parent hasn't been instantiated yet, which is why Preorder (Root first) is mandatory.
class TreeNode { int val; TreeNode left, right; TreeNode(int v){val=v;} }
public class TreeCloner {
public TreeNode cloneTree(TreeNode root) {
if (root == null) return null;
// 1. Process Root: Create the new parent node FIRST
TreeNode newRoot = new TreeNode(root.val);
// 2. Recursively clone and attach the left side
newRoot.left = cloneTree(root.left);
// 3. Recursively clone and attach the right side
newRoot.right = cloneTree(root.right);
return newRoot;
}
}Test your understanding of Preorder Traversal with these quick questions.
Question 1
What is the order of processing for Preorder Traversal?
Question 2
If you want to create an exact duplicate clone of a Binary Search Tree, which traversal should you use to read the original tree?
Question 3
In an iterative Preorder traversal using a Stack, which child must be pushed to the stack FIRST?
Preorder Traversal is a variation of Tree DFS where the root is processed BEFORE its children: Root (Self) -> Left Child -> Right Child.
Because it processes the parent node immediately upon discovery, Preorder is used whenever you need to aggressively explore or duplicate a tree structure.
If you traverse a tree using Preorder and insert those exact values sequentially into a new, blank tree, you will perfectly clone the original structure. If you try doing that with an Inorder traversal, you will end up creating a degenerate, unbalanced Linked List instead of a clone.
public class Preorder {
static class TreeNode {
int val; TreeNode left, right;
}
public void preorder(TreeNode root) {
if (root == null) return;
// 1. Process the Root (Self) FIRST
System.out.print(root.val + " ");
// 2. Go down the Left side
preorder(root.left);
// 3. Go down the Right side
preorder(root.right);
}
}What is Preorder Traversal primarily useful for?
Cloning a tree structure. By visiting the root first, you can instantiate the parent node in the clone tree before you attempt to attach the child nodes to it.
Cloning an exact binary tree structure into a new object in memory. You cannot attach a left child to a parent if the parent hasn't been instantiated yet, which is why Preorder (Root first) is mandatory.
class TreeNode { int val; TreeNode left, right; TreeNode(int v){val=v;} }
public class TreeCloner {
public TreeNode cloneTree(TreeNode root) {
if (root == null) return null;
// 1. Process Root: Create the new parent node FIRST
TreeNode newRoot = new TreeNode(root.val);
// 2. Recursively clone and attach the left side
newRoot.left = cloneTree(root.left);
// 3. Recursively clone and attach the right side
newRoot.right = cloneTree(root.right);
return newRoot;
}
}Test your understanding of Preorder Traversal with these quick questions.
Question 1
What is the order of processing for Preorder Traversal?
Question 2
If you want to create an exact duplicate clone of a Binary Search Tree, which traversal should you use to read the original tree?
Question 3
In an iterative Preorder traversal using a Stack, which child must be pushed to the stack FIRST?