Function to invert a given binary tree using preorder traversal
| 28 | |
| 29 | // Function to invert a given binary tree using preorder traversal |
| 30 | void invertBinaryTree(Node* root) |
| 31 | { |
| 32 | // base case: if the tree is empty |
| 33 | if (root == nullptr) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // swap left subtree with right subtree |
| 38 | swap(root->left, root->right); |
| 39 | |
| 40 | // invert left subtree |
| 41 | invertBinaryTree(root->left); |
| 42 | |
| 43 | // invert right subtree |
| 44 | invertBinaryTree(root->right); |
| 45 | } |
| 46 | |
| 47 | int main() |
| 48 | { |