mirror of tree.txt

来自「It is an ebook about trees」· 文本 代码 · 共 51 行

TXT
51
字号
Write a C program to create a mirror copy of a tree (left nodes become right and right nodes become left)! 

Discuss it!          


This C code will create a new mirror copy tree. 


mynode *copy(mynode *root) 
{ 
  mynode *temp; 
   
  if(root==NULL)return(NULL); 
    
  temp = (mynode *) malloc(sizeof(mynode)); 
  temp->value = root->value; 

  temp->left  = copy(root->right); 
  temp->right = copy(root->left); 

  return(temp); 
} 




This code will will only print the mirror of the tree 



void tree_mirror(struct node* node) 
{ 
   struct node *temp; 

   if (node==NULL) 
   { 
     return; 
   } 
   else 
   { 
      tree_mirror(node->left); 
      tree_mirror(node->right); 

      // Swap the pointers in this node 
      temp = node->left; 
      node->left = node->right; 
      node->right = temp; 
   } 
} 
 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?