-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
np.cpp
115 lines (93 loc) · 1.93 KB
/
np.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<iostream>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
typedef struct node N;
int search(int arr[], int strt, int end, int value);
struct node* newNode(int data);
struct node* buildTree(int in[], int pre[], int inStrt, int inEnd)
{
static int preIndex = 0;
if(inStrt > inEnd)
return NULL;
struct node *tNode = newNode(pre[preIndex++]);
if(inStrt == inEnd)
return tNode;
int inIndex = search(in, inStrt, inEnd, tNode->data);
tNode->left = buildTree(in, pre, inStrt, inIndex-1);
tNode->right = buildTree(in, pre, inIndex+1, inEnd);
return tNode;
}
int search(int arr[], int strt, int end, int value)
{
int i;
for(i = strt; i <= end; i++)
{
if(arr[i] == value)
return i;
}
}
struct node* newNode(int data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
void printPostorder(struct node* node)
{
if (node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
cout<<node->data;
}
int rtls(N *root, int val)// Root To Leaf Sum
{
if(root==NULL)
{
if(val==0)
return root->data;
else
return 0;
}
int total=val-root->data;
if(root->left==NULL && root->right==NULL && total==0)
return root->data;
else
return 0;
bool X,Y;
if(root->left)
X = rtls(root->left, total);
if(root->right)
Y = rtls(root->right, total);
if(X||Y)
return root->data;
}
int main()
{
int n,i,val;
cin>>n;
int in[n+1],pre[n+1];
for(i=0;i<n;i++)
{
cin>>in[i];
}
for(i=0;i<n;i++)
{
cin>>pre[i];
}
cin>>val;
struct node *root = buildTree(in, pre, 0, n - 1);
cout<<rtls(root,val);
getch();
return 0;
}