水下功夫做透,水上才能顺风顺水。

变动二叉树

定义二叉树结点

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);
      }
           
}                 

 

posted @ 2019-03-14 11:28  北方寒士  阅读(161)  评论(0)    收藏  举报