Here is the preorder:

private void preOrderTraverse(Node < E > node, int depth,
                                StringBuilder sb) {
    for (int i = 1; i < depth; i++) {
      sb.append("  ");
    }
    if (node == null) {
      sb.append("null\n");
    }
    else {
      sb.append(node.toString());
      sb.append("\n");
      preOrderTraverse(node.left, depth + 1, sb);
      preOrderTraverse(node.right, depth + 1, sb);
    }
  }

Would this be the postorder?:

private void postOrderTraverse(Node < E > node, int depth,
                                StringBuilder sb) {
    for (int i = 1; i < depth; i++) {
      sb.append("  ");
    }
    if (node == null) {
      sb.append("null\n");
    }
    else {
      preOrderTraverse(node.left, depth + 1, sb);
      preOrderTraverse(node.right, depth + 1, sb);
      sb.append(node.toString());
      sb.append("\n");
      
    }
  }

Nope I guess it would be:

private void postOrderTraverse(Node < E > node, int depth,
                                StringBuilder sb) {
    for (int i = 1; i < depth; i++) {
      sb.append("  ");
    }
    if (node == null) {
      sb.append("null\n");
    }
    else {
      postOrderTraverse(node.left, depth + 1, sb);
      postOrderTraverse(node.right, depth + 1, sb);
      sb.append(node.toString());
      sb.append("\n");
      
    }
  }

Reason being you will have to let the whole flow go in postorder, which will be:
> Process the left tree
> Process the right tree
> Process the current element.

Ya I did that before but my test program kept giving me a preOrder result so I just left it at preOrder.

It's weird.

I tried it that way with the postOrdered on my friends computer and I got postOrdered answer to show. But when I did it on my computer I got preOrdered. Even though the code was the exact samething in all areas including the test program

I don't get it...
Did you tried recompiling the whole stuff and making sure that your IDE is not messing up somewhere?

Anyways, is this solved now? or you need some more help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.