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?
#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 << " ";
}
}
Comments
Leave a comment