MCPcopy Create free account
hub / github.com/SR-Sunny-Raj/Hacktoberfest2021-DSA / invertBinaryTree

Function invertBinaryTree

14. Tree/invert_binary_tree.cpp:30–45  ·  view source on GitHub ↗

Function to invert a given binary tree using preorder traversal

Source from the content-addressed store, hash-verified

28
29// Function to invert a given binary tree using preorder traversal
30void 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
47int main()
48{

Callers 1

mainFunction · 0.85

Calls 1

swapFunction · 0.50

Tested by

no test coverage detected