두 갈래 나무의 후순과 중순 결과로 선순 결과를 구하다

1604 단어
귀속:
    #include<iostream>  
    #include<cstdio>  
    #include<string>  
    using namespace std;   
    string x,y;  
      
    void write(int p,int q,int m,int n)  
    {  
      if(p>q)return;  
      if(p==q){cout<<x[p];return;}  
        
      int i,j,k;  
      k=x.find(y[n]);  //  
      cout<<y[n];  // 
	  //  
      write(p,k-1,m,m+k-p-1);  
      write(k+1,q,m+k-p,n-1);  
    }  
    int main()  
    {  
      int i,j,k;  
      cin>>x>>y;  
      k=x.length();  
      write(0,k-1,0,k-1);  
      return 0;  
    }  

비귀속:
#include <iostream>
#include<string.h>
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;
    }
  TreeNode* node = new TreeNode;
  node->elem = *(postorder+length-1);//  
  cout<<node->elem;
  int rootIndex = 0;
  for(;rootIndex <length; rootIndex++)
    {
      if(inorder[rootIndex] == *(postorder+length-1))//  
      break;
    }
  //  
  node->left = BinaryTreeFromOrderings(inorder, postorder, rootIndex);
  //  
  node->right = BinaryTreeFromOrderings(inorder + rootIndex + 1, postorder + rootIndex, length - (rootIndex+1));
  return node;
}


int main()
{
	char* in;
	char* po;
	int len;
	cin>>in;
	cin>>po;
	len=strlen(in);	
    BinaryTreeFromOrderings(in, po, len);
    printf("
"); return 0; }

좋은 웹페이지 즐겨찾기