Posted on 1999-12-22
Initially I asked this Question:
How would I write a function in c++ that will interchange all left and right subtrees in a linked binary tree.
/ \ / \
2 3 becomes --> 3 2
/ / \ / \ \
4 5 6 6 5 4
/ \ / \
7 8 8 7
I don't need the entire program, just the required function segment of code.
the definition of the tree node I have is:
typedef struct treenode
struct treenode *left;
struct treenode *right;
typedef TreeNode *TreePointer;
And I was given this answer:
void InterChange(TreeNode *tN)
TreeNode *left = tN->left; //get left node
TreeNode *right = tN->right; //grt right node
if (left || right) //not last?
tN->left = right; //left = write
tN->right = left; //righ =left
if (tN->left) //exist left?
InterChange(tN->left); //make interchange inside // left(recursion)
if (tN->right) //exist right?
InterChange(tN->right); //make interchange inside //right(recursion)
So the typedef TreeNode *TreePointer; after the structure is not needed or used in the InterChange function??