Category: Parentheses-Problems

Construct a Binary Tree from String with bracket representation | Set 2

  #include using namespace std;  struct Node {    Node* left;    Node* right;    int data;              Node(int element)    {        data = element;        this->left = nullptr;        this->right = nullptr;    }};  void preorder(Node* root){    if (!root)        return;      cout data left);    preorder(root->right);}  Node* constructTree(string s){          Node* root = new Node(s[0] – ‘0’);              stack stk;          for (int i = 1; i < s.length(); i++) {                  if (s[i] == '(') {                          stk.push(root);        }                  else if (s[i] == ')') {                                      root = stk.top();                          stk.pop();        }                  else {                                                  if (root->left == nullptr) {                  Node* […]

Continue Reading