You are viewing a single comment's thread. Return to all comments →
cpp solution with recursion :
int height(Node* root) { int level = 0; if (root == nullptr) { return level; } else { int lHeight = height(root->left); int rHeight = height(root->right); if (lHeight > rHeight) { return (lHeight + 1); } else { return (rHeight + 1); } } return level; } void printCurrentLevel(Node* node, int i) { if (node == nullptr) { return; } if (i == 1) { cout << node->data << " "; } else if (i > 1) { printCurrentLevel(node->left,i-1); printCurrentLevel(node->right,i-1); } } void levelOrder(Node * root) { int h = height(root); int i; for (i = 1; i <= h; i++) { printCurrentLevel(root, i); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Level Order Traversal
You are viewing a single comment's thread. Return to all comments →
cpp solution with recursion :