1
+ // { Driver Code Starts
2
+ // Initial Template for C++
3
+
4
+ #include < bits/stdc++.h>
5
+ using namespace std ;
6
+
7
+ struct Node
8
+ {
9
+ int data;
10
+ struct Node *left;
11
+ struct Node *right;
12
+
13
+ Node (int x)
14
+ {
15
+ data = x;
16
+ left = NULL ;
17
+ right = NULL ;
18
+ }
19
+ };
20
+
21
+ void printInorder (Node *node)
22
+ {
23
+ if (node == NULL )
24
+ {
25
+ return ;
26
+ }
27
+ printInorder (node->left );
28
+ cout << node->data << " " ;
29
+ printInorder (node->right );
30
+ }
31
+ Node *buildTree (string str)
32
+ {
33
+ // Corner Case
34
+ if (str.length () == 0 || str[0 ] == ' N' )
35
+ return NULL ;
36
+
37
+ // Creating vector of strings from input
38
+ // string after spliting by space
39
+ vector<string> ip;
40
+
41
+ istringstream iss (str);
42
+ for (string str; iss >> str;)
43
+ ip.push_back (str);
44
+
45
+ // Create the root of the tree
46
+ Node *root = new Node (stoi (ip[0 ]));
47
+
48
+ // Push the root to the queue
49
+ queue<Node *> queue;
50
+ queue.push (root);
51
+
52
+ // Starting from the second element
53
+ int i = 1 ;
54
+ while (!queue.empty () && i < ip.size ())
55
+ {
56
+
57
+ // Get and remove the front of the queue
58
+ Node *currNode = queue.front ();
59
+ queue.pop ();
60
+
61
+ // Get the current node's value from the string
62
+ string currVal = ip[i];
63
+
64
+ // If the left child is not null
65
+ if (currVal != " N" )
66
+ {
67
+
68
+ // Create the left child for the current Node
69
+ currNode->left = new Node (stoi (currVal));
70
+
71
+ // Push it to the queue
72
+ queue.push (currNode->left );
73
+ }
74
+
75
+ // For the right child
76
+ i++;
77
+ if (i >= ip.size ())
78
+ break ;
79
+ currVal = ip[i];
80
+
81
+ // If the right child is not null
82
+ if (currVal != " N" )
83
+ {
84
+
85
+ // Create the right child for the current node
86
+ currNode->right = new Node (stoi (currVal));
87
+
88
+ // Push it to the queue
89
+ queue.push (currNode->right );
90
+ }
91
+ i++;
92
+ }
93
+
94
+ return root;
95
+ }
96
+
97
+
98
+ // } Driver Code Ends
99
+ // User function Template for C++
100
+
101
+ /*
102
+ structure of the node of the binary tree is as
103
+ struct Node
104
+ {
105
+ int data;
106
+ struct Node *left;
107
+ struct Node *right;
108
+
109
+ Node(int x)
110
+ {
111
+ data = x;
112
+ left = NULL;
113
+ right = NULL;
114
+ }
115
+ };
116
+ */
117
+ class Solution
118
+ {
119
+ public:
120
+
121
+ void solve (Node* root, int sum, int &maxSum, int len, int &maxLen)
122
+ {
123
+ if (!root)
124
+ {
125
+ if (len > maxLen)
126
+ {
127
+ maxLen = len;
128
+ maxSum = sum;
129
+ }
130
+ else if (len == maxLen)
131
+ {
132
+ maxSum = max (sum, maxSum);
133
+ }
134
+ return ;
135
+ }
136
+
137
+ sum += root->data ;
138
+ solve (root->left , sum, maxSum, len+1 , maxLen);
139
+ solve (root->right , sum, maxSum, len+1 , maxLen);
140
+
141
+ }
142
+
143
+ int sumOfLongRootToLeafPath (Node *root)
144
+ {
145
+ int len=0 ;
146
+ int maxLen=0 ;
147
+
148
+ int sum=0 ;
149
+ int maxSum=0 ;
150
+
151
+ solve (root, sum, maxSum, len, maxLen);
152
+
153
+ return maxSum;
154
+ }
155
+ };
156
+
157
+ // { Driver Code Starts.
158
+
159
+ int main ()
160
+ {
161
+
162
+ int t;
163
+ scanf (" %d" , &t);
164
+ cin.ignore ();
165
+ while (t--)
166
+ {
167
+ string treeString;
168
+ getline (cin, treeString);
169
+ Node *root = buildTree (treeString);
170
+ Solution obj;
171
+ int res = obj.sumOfLongRootToLeafPath (root);
172
+ cout << res << " \n " ;
173
+ }
174
+ return 0 ;
175
+ }
176
+ // } Driver Code Ends
0 commit comments