Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2ad61f1

Browse files
committed
add new codes
1 parent c3adb95 commit 2ad61f1

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

‎a.out‎

336 Bytes
Binary file not shown.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// https://www.geeksforgeeks.org/construct-a-binary-tree-from-postorder-and-inorder/
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
struct Node{
6+
int data;
7+
Node *left , *right;
8+
};
9+
10+
Node *getNode(int data){
11+
Node *temp = new Node();
12+
temp->data = data;
13+
temp->left = temp->right = NULL;
14+
}
15+
16+
int search(int in[], int data, int l, int r){
17+
for(int i=l ;i<=r; i++){
18+
if(in[i] == data){
19+
return i;
20+
}
21+
}
22+
return -1;
23+
}
24+
25+
Node *buildTree(int in[], int post[], int inStart, int inEnd,int n){
26+
static int postIndex = n-1;
27+
if(inStart > inEnd)
28+
return NULL;
29+
Node *root = getNode(post[postIndex]);
30+
postIndex -= 1;
31+
if(inStart == inEnd)
32+
return root;
33+
34+
int inIndex = search(in,root->data,inStart, inEnd);
35+
if(inIndex == -1){
36+
cout << "Error\n";
37+
return NULL;
38+
}
39+
40+
root->right = buildTree(in, post, inIndex+1, inEnd,n);
41+
root->left = buildTree(in,post,inStart,inIndex-1,n);
42+
return root;
43+
44+
}
45+
46+
void inorder(Node *root){
47+
if(!root) return;
48+
inorder(root->left);
49+
cout << root->data << " ";
50+
inorder(root->right);
51+
}
52+
53+
int main(){
54+
int post[] = {8,4,5,2,6,7,3,1};
55+
int in[] = {4,8,2,5,1,6,3,7};
56+
Node *root = buildTree(in,post,0,7,8);
57+
inorder(root);
58+
return 0;
59+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// https://www.geeksforgeeks.org/construct-tree-from-given-inorder-and-preorder-traversal/
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
struct Node{
6+
char data;
7+
Node *left , *right;
8+
};
9+
10+
Node *getNode(char data){
11+
Node *temp = new Node();
12+
temp->data = data;
13+
temp->left = temp->right = NULL;
14+
}
15+
16+
int search(int in[], int data, int l, int r){
17+
for(int i=l ;i<=r; i++){
18+
if(in[i] == data){
19+
return i;
20+
}
21+
}
22+
return -1;
23+
}
24+
25+
Node *buildTree(int in[], int pre[], int inStart, int inEnd){
26+
static int preIndex = 0;
27+
if(inStart > inEnd)
28+
return NULL;
29+
Node *root = getNode(pre[preIndex]);
30+
preIndex += 1;
31+
if(inStart == inEnd)
32+
return root;
33+
34+
int inIndex = search(in,root->data,inStart, inEnd);
35+
if(inIndex == -1){
36+
cout << "Error\n";
37+
return NULL;
38+
}
39+
root->left = buildTree(in,pre,inStart,inIndex-1);
40+
root->right = buildTree(in, pre, inIndex+1, inEnd);
41+
return root;
42+
43+
}
44+
45+
void inorder(Node *root){
46+
if(!root) return;
47+
inorder(root->left);
48+
cout << root->data << " ";
49+
inorder(root->right);
50+
}
51+
52+
int main(){
53+
int pre[] = {'A','B','D','E','C','F'};
54+
int in[] = {'D','B','E','A','F','C'};
55+
Node *root = buildTree(in,pre,0,5);
56+
inorder(root);
57+
return 0;
58+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /