Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions C++/Construct_Binary_Tree_From_Bracket_Parenthesis.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include<bits/stdc++.h>
using namespace std;

class Solution{
public:
Node *treeFromString(string str){
string cur = "";
int i = 0, num, n = str.length();
while (i < n && str[i] != '(' && str[i] != ')') {
cur += str[i++];
}
num = stoi(cur);
Node *root = new Node(num);
stack<Node*> st;
st.push(root);
for (; i < n; i++) {
if (str[i] == '(') {
cur = "";
while (str[i + 1] != '(' && str[i + 1] != ')') {
i++;
cur += str[i];
}
num = stoi(cur);
Node *temp = new Node(num);
if (!st.top()->left)
st.top()->left = temp;
else
st.top()->right = temp;
st.push(temp);
}
else if (str[i] == ')')
st.pop();
}
return root;
}
};

int main(){
int t;cin>>t;
while(t--){
string s;cin>>s;
Solution obj;
Node *root = obj.treeFromString(s);
}
return 0;
}