Answer to Question #308062 in C++ for ahmed

Question #308062

Why must we check before the call to preorder when implementing as a method, whereas we could check inside the call when implementing as a function?


1
Expert's answer
2022-03-08T16:52:52-0500
#include <iostream>

struct node {
  int value;
  struct node *left;
  struct node *right;
}

void preOrderTraversal(struct node *nd) {
  if (nd) {
    cout << nd->value << " ";
    preOrderTraversal(nd->left);
    preOrderTraversal(nd->right);
  }
}

void inOrderTraversal(struct node *nd) {
  if (nd) {
    inOrderTraversal(nd->left);
    cout << nd->value << " ";
    inOrderTraversal(nd->right);
  }
}

void postOrderTraversal(struct node *nd) {
  if (nd) {
    postOrderTraversal(nd->left);
    postOrderTraversal(nd->right);
    cout << nd->value << " ";
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog