The pre order goes like this:
- Visit the current node
- If exists left node, visit pre order of left node.
- If exist right node, visit pre order of right node.
I’m sure that with these few steps you will be able to implement your pre order method.
A pseudocode of this:
function preOrder(Node node)
//1. visit current node
show(node->data)
//2. if exists left node, visit pre order of left node
Node left = node->left
if (left is not null)
preOrder(left)
//3. if exist right node, visit pre order of right node.
Node right = node->left
if (right is not null)
preOrder(right)
Since this is homework, the implementation is up to you.
10
solved How do I write the Preorder Method for Binary Trees? [closed]