C++实现的构建二叉树

1. 由前序和中序构造二叉树

#include 
#include 

/**
 * Definition for a binary tree node.*/
 
  struct TreeNode {
     int val;
      struct TreeNode *left;
      struct TreeNode *right;
  };
  
struct TreeNode* doBuildTree(int* preorder, int preorderSize, int* inorder, int inorderSize, int preIndexStart,int preIndexEnd, int inIndexStart, int inIndexEnd) {   
     printf(" preIndexStart=%d, inIndexStart=%d,inIndexEnd=%d ",preIndexStart,inIndexStart,inIndexEnd);
    if(inIndexStart < 0 || inIndexEnd >= inorderSize || inIndexEnd < inIndexStart ||preIndexEnd >= preorderSize) return NULL;
  
    struct TreeNode* root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    root->val = preorder[preIndexStart];
    printf(" root=%d",root->val);
    int mid = -1;
    int i=inIndexStart; 
    while(i<=inIndexEnd){
        if(preorder[preIndexStart] == inorder[i]){
            mid = i;
            break;
        }
        i++;
    }    
    printf(" mid=%d \n",mid);
    root->left =  doBuildTree(preorder, preorderSize, inorder, inorderSize,preIndexStart+1,preIndexStart+mid-inIndexStart, inIndexStart,mid-1);
    root->right = doBuildTree(preorder, preorderSize, inorder, inorderSize,preIndexStart+mid-inIndexStart+1,preIndexEnd,mid+1,inIndexEnd);
    return root;    
}

struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) {
    if(preorderSize != inorderSize) return NULL;
    if(inorderSize <= 0) return NULL;
    return doBuildTree(preorder, preorderSize, inorder, inorderSize,0,preorderSize-1,0,inorderSize-1);
    
}

void preOrderTraverse(TreeNode* node){
    if (node != NULL) {
            printf("%d ",node->val);
            preOrderTraverse(node->left);
            preOrderTraverse(node->right);
        } else {
            return;
        }
      
}
int main(void) { 
	int a[] = {1,2,3} ,b[] = {1,3,2};
	TreeNode* root = buildTree(a,3,b,3);
	printf("\n====\n");
	preOrderTraverse(root);
	return 0;
}

2.由中序和后序构造二叉树

#include 
#include 

/**
 * Definition for a binary tree node.*/
 
  struct TreeNode {
     int val;
      struct TreeNode *left;
      struct TreeNode *right;
  };
  
struct TreeNode* doBuildTree(int* inorder, int inorderSize, int* postorder, int postorderSize, int inIndexStart,int inIndexEnd, int postIndexStart, int postIndexEnd) {   
     printf(" postIndexStart=%d,postIndexEnd=%d, inIndexStart=%d,inIndexEnd=%d ",postIndexStart,postIndexEnd,inIndexStart,inIndexEnd);
    if(inIndexStart < 0 ||inIndexEnd<0|| inIndexEnd >= inorderSize || inIndexEnd < inIndexStart ||postIndexStart >=postorderSize || postIndexEnd <0) return NULL;
  
    struct TreeNode* root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    root->val = postorder[postIndexEnd];
    printf(" root=%d",root->val);
    int mid = -1;
    int i=inIndexStart,j=0; 
    while(i<=inIndexEnd){
        if(postorder[postIndexEnd] == inorder[i]){
            mid = i;
            break;
        }
        i++;
    }
    
    printf(" mid=%d \n",mid);
    root->left = doBuildTree(inorder, inorderSize, postorder, postorderSize,inIndexStart, mid-1,postIndexStart,postIndexStart+mid-inIndexStart-1);
    root->right =doBuildTree(inorder, inorderSize, postorder, postorderSize,mid+1,inIndexEnd,postIndexStart+mid-inIndexStart,postIndexEnd-1);
    return root;    
}

struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize) {
    if(postorderSize != inorderSize) return NULL;
    if(postorderSize <= 0) return NULL;
    return doBuildTree(inorder, inorderSize, postorder, postorderSize,0,inorderSize-1,0,postorderSize-1);
    
}

void preOrderTraverse(TreeNode* node){
    if (node != NULL) {
            printf("%d ",node->val);
            preOrderTraverse(node->left);
            preOrderTraverse(node->right);
        } else {
            return;
        }
      
}
int main(void) { 
	int a[] = {1,3,2} ,b[] = {3,2,1};
	TreeNode* root = buildTree(a,3,b,3);
	printf("\n====\n");
	preOrderTraverse(root);
	return 0;
}

3. 仅有前序和后序无法构建二叉树。

你可能感兴趣的:(算法)