二叉树遍历

循环:利用叶子结点右指针为空的特点,给叶子结点设置其直接后继,输出完孩子结点后,再返回其直接后继;

  1. void midPrint_m(TreeNode *root)  
  2. {  
  3.     TreeNode *cur = root;  
  4.     TreeNode *pre = NULL;  
  5.     while(cur != NULL)  
  6.     {  
  7.         if(cur->left == NULL)  
  8.         {//左孩子为空,则直接输出  
  9.             cout<<cur->var<<" ";  
  10.             cur = cur->right;  
  11.         }  
  12.         else  
  13.         {  
  14.             pre = cur->left;  
  15.             while( pre->right != NULL && pre->right != cur )  
  16.             {//找到cur的直接前驱  
  17.                 pre = pre->right;  
  18.             }  
  19.             if(pre->right == NULL)  
  20.             {//设置后继结点  
  21.                 pre->right = cur;  
  22.                 cur = cur->left;  
  23.             }  
  24.             else  
  25.             {  
  26.                 pre->right = NULL;//重新设置为空  
  27.                 cout<<cur->var<<" ";  
  28.                 cur = cur->right;  
  29.             }  
  30.         }  
  31.     }  
  32. }  
  33.   
  34. void prePrint_m(TreeNode *root)  
  35. {//基本同于中遍历  
  36.     TreeNode *cur = root;  
  37.     TreeNode *pre = NULL;  
  38.     while(cur != NULL)  
  39.     {  
  40.         if(cur->left == NULL)  
  41.         {  
  42.             cout<<cur->var<<" ";  
  43.             cur = cur->right;  
  44.         }  
  45.         else  
  46.         {  
  47.             pre = cur->left;  
  48.             while( pre->right != NULL && pre->right != cur )  
  49.             {  
  50.                 pre = pre->right;  
  51.             }  
  52.             if(pre->right == NULL)  
  53.             {  
  54.                 pre->right = cur;  
  55.                 cout<<cur->var<<" ";  
  56.                 cur = cur->left;  
  57.             }  
  58.             else  
  59.             {  
  60.                 pre->right = NULL;  
  61.                 cur = cur->right;  
  62.             }  
  63.         }  
  64.     }  
  65. }  
  66. //this is the most difficult algorithm  
  67. void reverse_out(TreeNode *from,TreeNode *to)  
  68. {  
  69.     //first reverse from->to  
  70.     //reverse  
  71.     TreeNode *cur = from;  
  72.     TreeNode *post = cur->right;  
  73.     while(cur != to)  
  74.     {  
  75.         TreeNode *tmp = post->right;  
  76.         post->right = cur;  
  77.         cur = post;  
  78.         post = tmp;  
  79.     }  
  80.     //already reverse,output list  
  81.     TreeNode *traversal = cur;  
  82.     while( cur != from )  
  83.     {  
  84.         cout<<cur->var<<" ";  
  85.         cur = cur->right;  
  86.     }  
  87.     cout<<cur->var<<" ";  
  88.     //reverse original  
  89.     cur = to;  
  90.     post = cur->right;  
  91.     while(cur != from)  
  92.     {  
  93.         TreeNode *tmp = post->right;  
  94.         post->right = cur;  
  95.         cur = post;  
  96.         post = tmp;  
  97.     }  
  98.     //restore to's right  
  99.     to->right = NULL;  
  100. }  
  101. void postPrint_m(TreeNode *root)  
  102. {  
  103.     TreeNode *newroot = new TreeNode(0);  
  104.     newroot->left = root;  
  105.     newroot->right = NULL;  
  106.     TreeNode *cur = newroot;  
  107.     TreeNode *pre = NULL;  
  108.     while(cur != NULL)  
  109.     {  
  110.         if(cur->left == NULL)  
  111.         {  
  112.             cur = cur->right;  
  113.         }  
  114.         else  
  115.         {  
  116.             pre = cur->left;  
  117.             while(pre->right != NULL && pre->right != cur)  
  118.             {  
  119.                 pre = pre->right;  
  120.             }  
  121.             if(pre->right == NULL)  
  122.             {  
  123.                 pre->right = cur;  
  124.                 cur = cur->left;  
  125.             }  
  126.             else  
  127.             {  
  128.                 pre->right = NULL;  
  129.                 reverse_out(cur->left,pre);  
  130.                 cur = cur->right;  
  131.             }  
  132.         }  
  133.     }  
原文地址:https://www.cnblogs.com/fchy822/p/8999665.html