[Solved] How do I write the Preorder Method for Binary Trees? [closed]


The pre order goes like this:

  1. Visit the current node
  2. If exists left node, visit pre order of left node.
  3. 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]