Answer to Question #304519 in C++ for hamza

Question #304519

Consider the two different techniques we used for implementing traversals of a binary


tree. 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-02T01:16: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 << " ";
  }
}

int main() { return 0; }

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