# Thread: Flipping a binary tree using recursion

1. ## Flipping a binary tree using recursion

can anyone give me some ideas on how i could flip a binary tree around using a recursive function..for example.

struct BinaryTreeNode
{
int data;
BinaryTreeNode *left;
BinaryTreeNode *right;
};

Example original tree: Example new tree:
1 1
/ \ / \
2 3 3 2
/ \ / \
4 5 5 4

2. old tree
1
/\
2 3
/\
4 5

new tree
1
/\
3 2
/\
5 4

3. is it a binary tree? binary tree should be
1
\
2
\
3
\
4
\
5

4. Unregistered said:

is it a binary tree? binary tree should be
1
\
2
\
3
\
4
\
5
The way a binary tree looks depends on the order the items are added to a tree. If the numbers were added in order (least to greatest), then yes the tree would look like you describe.

5. Copy the tree, then either do a pre-order traversal on the source copying to the destination by means of a post-order traversal, or do a post-order traversal of the source and a pre-order traversal of the destination.

6. Originally posted by hk_mp5kpdw
Unregistered said:

The way a binary tree looks depends on the order the items are added to a tree. If the numbers were added in order (least to greatest), then yes the tree would look like you describe.
...which is where the height balancing comes in.

7. Using a binary tree you can insert elements any way you like. A binary search tree orders the elements on insertion in some pre-defined manner.

8. I have some code that could help. Let's see your code first.

When I think height balancing I think AVL trees not binary trees.!!!

Mr. c.