Resume
Is that possible to reverse a binary tree in O(1)? Yes!!
Let’s do it right now!
Code
struct NormalNode {
int value;
struct NormalNode *left;
struct NormalNode *right;
};
struct ReversedNode {
int value;
struct ReversedNode *right;
struct ReversedNode *left;
};
struct ReversedNode *reverseTree(struct NormalNode *root) {
return (struct ReversedNode *)root;
}
Test
Here we will test if it works.
We will create a binary tree on the left, then we will reverse it and make it become the right one.
#include