Construct a binary tree
Never
#include <stdlib.h> #include <stdio.h> typedef struct _node { int value; struct _node *left; struct _node *right; } Node; Node *create_node(int value){ Node *newnode = (Node*)malloc(sizeof(Node)); newnode->left = NULL; newnode->right = NULL; newnode->value = value; return newnode; } void postorder(Node *root){ if(root == NULL) return; postorder(root->left); postorder(root->right); printf("%d ",root->value); } void destroy(Node* root){ if(root == NULL)return; destroy(root->left); destroy(root->right); free(root); } /* Parameter description: int *inorder : the inorder traversal sequence of the binary tree. int *preorder : the preorder traversal sequence of the binary tree. int inorder_start : the starting index of the inorder traversal of the subtree. int inroder_end : the ending index of the inorder traversal of the subtree. As for the preorder traversal index, try declaring a static variable inside this function. */ Node *build(int *inorder, int *preorder, int inorder_start, int inorder_end){ static int preIndex = 0; if(inorder_start > inorder_end)return NULL; Node *node = create_node(preorder[preIndex++]); if(inorder_start == inorder_end) return node; int i; for(i=inorder_start;i<inorder_end;i++){ if(inorder[i] == node->value) break; } node->left = build(inorder,preorder,inorder_start,i-1); node->right = build(inorder,preorder,i+1,inorder_end); } int main(void){ int n; scanf("%d",&n); int *inorder = (int*)malloc(sizeof(int)*n); int *preorder = (int*)malloc(sizeof(int)*n); for(int i=0;i<n;i++){ scanf("%d",&inorder[i]); } for(int i=0;i<n;i++){ scanf("%d",&preorder[i]); } Node *root = build(inorder,preorder,0,n-1); postorder(root); printf("\n"); destroy(root); free(inorder); free(preorder); return 0; }
Raw Text
-
✅✅ Spy cam, incest, banned videos ✅✅
10 min ago
-
asdfnm adsdfgdasfdf
1 hour ago
-
sdddsdssddsdsf
1 hour ago
-
sdfgasdf sadfsadf
1 hour ago
-
asdnb nadsdfdasd
2 hours ago
-
Untitled
2 hours ago
-
BUY SUPER HIGH QUALITY COUNTERFEIT MONEY ,CLONE CREDIT CARDS ONLINE GBP, DOLLAR, EUROS BUY COUNTERFE
CSS | 2 hours ago
-
BUY SUPER HIGH QUALITY COUNTERFEIT MONEY ,CLONE CREDIT CARDS ONLINE GBP, DOLLAR, EUROS BUY COUNTERFE
CSS | 2 hours ago
-
fghjfgh fghjfghj
2 hours ago
-
dsfdsf dsfdfs fsdd fsdfsfddf
2 hours ago