Using the binary_tree_node, write a recursive function to meet the following spe
ID: 3816370 • Letter: U
Question
Using the binary_tree_node, write a recursive function to meet the following specification. Check as much of the precondition as possible.
template <class Item>
void flip(binary_tree_node<Item>* root_ptr)
// Precondition: root_ptr is the root pointer of a non-empty binary tree.
// Postcondition: The tree is now the mirror image of its original value.
// Example original tree: Example new tree:
// 1 1
// / /
// 2 3 3 2
// / /
// 4 5 5 4
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
void flip(binary_tree_node<Item>* root_ptr)
{
if(root_ptr == NULL)
return
swap(root_ptr->left, root_ptr->right); //most essential step- swap the two children
flip(root_ptr->left); // do this swapping recursively.
flip(root_ptr->right);
}
sample i/p:
1
/
2 3
Sample op
1
/
3 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.