已知二叉树的中序遍历和后序遍历,如何求前序遍历

    

     在已知二叉树的前序遍历和中序遍历的情况下,求出了后序遍历。

那么,对称地,如果已知二叉树的后序遍历和中序遍历,如何求前序遍历呢?

其实思路与上文完全类似。

代码如下:

  1. // InPost2Pre.cpp : Defines the entry point for the console application. 
  2. // 
  3.  
  4. #include "stdafx.h" 
  5. #include <iostream> 
  6. using namespace std; 
  7.  
  8. struct TreeNode 
  9.   struct TreeNode* left; 
  10.   struct TreeNode* right; 
  11.   char  elem; 
  12. }; 
  13.  
  14. TreeNode* BinaryTreeFromOrderings(char* inorder,char* postorder, int length) 
  15.   if(length == 0) 
  16.     { 
  17.       return NULL; 
  18.     } 
  19.   //We have root then; 
  20.   TreeNode* node = new TreeNode;//Noice that [new] should be written out. 
  21.   node->elem = *(postorder+length-1); 
  22.   cout<<node->elem<<endl; 
  23.   int rootIndex = 0; 
  24.   for(;rootIndex <length; rootIndex++) 
  25.     { 
  26.       if(inorder[rootIndex] == *(postorder+length-1)) 
  27.       break
  28.     } 
  29.   //在循环条件而非循环体完成了对变量值的改变。 
  30.   //Left 
  31.   node->left = BinaryTreeFromOrderings(inorder, postorder, rootIndex); 
  32.   //Right 
  33.   node->right = BinaryTreeFromOrderings(inorder + rootIndex + 1, postorder + rootIndex, length - (rootIndex+1)); 
  34.   //node->right = *(postorder+length-2); 
  35.  
  36.   return node; 
  37.  
  38.  
  39. int main(int argc,char* argv[]) 
  40.     printf("Hello World!\n"); 
  41.     char* in="ADEFGHMZ"
  42.     char* po="AEFDHZMG";     
  43.         BinaryTreeFromOrderings(in, po, 8); 
  44.         printf("\n"); 
  45.     return 0; 
// InPost2Pre.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

struct TreeNode
{
  struct TreeNode* left;
  struct TreeNode* right;
  char  elem;
};

TreeNode* BinaryTreeFromOrderings(char* inorder, char* postorder, int length)
{
  if(length == 0)
    {
      return NULL;
    }
  //We have root then;
  TreeNode* node = new TreeNode;//Noice that [new] should be written out.
  node->elem = *(postorder+length-1);
  cout<<node->elem<<endl;
  int rootIndex = 0;
  for(;rootIndex <length; rootIndex++)
    {
      if(inorder[rootIndex] == *(postorder+length-1))
      break;
    }
  //在循环条件而非循环体完成了对变量值的改变。
  //Left
  node->left = BinaryTreeFromOrderings(inorder, postorder, rootIndex);
  //Right
  node->right = BinaryTreeFromOrderings(inorder + rootIndex + 1, postorder + rootIndex, length - (rootIndex+1));
  //node->right = *(postorder+length-2);

  return node;
}


int main(int argc, char* argv[])
{
	printf("Hello World!\n");
	char* in="ADEFGHMZ";
	char* po="AEFDHZMG";	
        BinaryTreeFromOrderings(in, po, 8);
        printf("\n");
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值