Mark As Completed Discussion

Preorder traversal

  1. Visit root node
  2. Then go to all the nodes in the left subtree
  3. Visit all the nodes in the right subtree

When to use? You can use pre-order traversal to create a copy of the tree, since you can access each node before its subtrees. It's also used to get expressions on an expression tree.

SNIPPET
1display(root.data)
2preorder(root.left)
3preorder(root.right)
Step Three

Note: This algorithm is equivalent to the famous graph algorithm Depth First Search (DFS).

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment