定义二叉树结点
typedef struct Node{ struct Node *left; char data; struct Node *right; struct Node *next }BTNode;
//添加兄弟指针 void addSiblingPtr(BTNode* root){ if(root==null){ return; } if(root->left!=null){ if(root->right!=null){ root->left->next = root->right; } } if(root->right!=null){ if(root->next!=null && root->next->left!=null){ root->right->next = root->next->left; } } addSiblingPtr(root->left); addSiblingPtr(root->right); }
思路:逐层添加。
//求二叉树的镜像 void mirror(BTNode* root){ if(NULL==root) { return; } if (root->lchild == NULL && root->rchild == NULL){ return; } BTNode* tmp = root->lchild; root->lchild = root->rchild; root->rchild = tmp; if(root->lchild!=NULL){ mirror(root->lchild); } if(root->rchild!=NULL){ mirror(root->rchild); } }