-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_ConstructBinaryTreeFromStringWithBracketRepresentation.cpp
168 lines (145 loc) · 3.45 KB
/
GFG_ConstructBinaryTreeFromStringWithBracketRepresentation.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
https://leetcode.com/problems/construct-binary-tree-from-string/
https://practice.geeksforgeeks.org/problems/construct-binary-tree-from-string-with-bracket-representation/0/
https://www.lintcode.com/problem/880/
*/
//GFG ---------------------------------------
// { Driver Code Starts
//Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
/* A binary tree node has data, pointer to left
child and a pointer to right child */
class Node
{
public:
int data;
Node *left, *right;
Node(int data)
{
this->data = data;
this->left = this->right = NULL;
}
};
/* This function is to print the inorder of the tree */
void inorder(Node *root)
{
if (root == NULL)
return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
// } Driver Code Ends
//User function Template for C++
class Solution
{
public:
Node *treeFromString(string str, int idx=0)
{
if(str.empty())
return nullptr;
int i = 0;
return str2tree(str, i);
}
Node* str2tree(string& s, int& idx)
{
if(idx >= s.length())
return nullptr;
string num="";
while(idx<s.length() and s[idx]!='(' and s[idx] != ')')
{
num+=s[idx];
idx++;
}
Node* node = new Node(stoi(num));
if(idx < s.length() and s[idx] == '(')
{
idx++; // remove (
node->left = str2tree(s, idx);
idx++; // remove )
// cout<<node->data<<" "<<idx<<endl;
// if again ( then it is right subtree
if(idx < s.size() and s[idx] == '(')
{
idx++;
node->right = str2tree(s, idx);
idx++;
}
}
return node;
}
};
// { Driver Code Starts.
// Driver Code
int main()
{
int t;
cin >> t;
while (t--)
{
string str;
cin >> str;
Solution obj;
Node *root = obj.treeFromString(str);
inorder(root);
cout << "\n";
}
return 0;
}
// } Driver Code Ends
// LintCode -----------------------------------------------------------
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param s: a string
* @return: a root of this tree
*/
TreeNode* str2tree(string &s) {
// write your code here
if(s=="")
return nullptr;
int open = 0;
while(open<s.size() and s[open] != '(' and s[open] != ')')
open++;
int data = stoi(s.substr(0, open));
TreeNode *node = new TreeNode(data);
s.erase(0, open);
// cout<<node->val<<" L "<<s<<endl;
if(s[0] == '(')
{
s.erase(0, 1);
node->left = str2tree(s);
}
if(s[0] == ')')
{
s.erase(0, 1);
return node;
}
// cout<<node->val<<" R "<<s<<endl;
if(s[0] == '(')
{
s.erase(0, 1);
node->right = str2tree(s);
}
if(s[0] == ')')
{
s.erase(0, 1);
return node;
}
return node;
}
};
//"1(2(3(4(5(6(7(8)))))))(9(10(11(12(13(14(15)))))))"