
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Construct Binary Tree from String in C++
Suppose we have a string consisting of parenthesis and integers. We have to construct a binary tree from that string. The whole input represents a binary tree. It holds an integer that followed by zero, one or two pairs of parentheses. The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure.
So, if the input is like "4(2(3)(1))(6(5))", then the output will be [3,2,1,4,5,6] (inorder traversal)
To solve this, we will follow these steps −
Define a function solve(), this will take s, idx,
if idx >= size of s, then −
return null
num := empty string
while (idx < size of s and s[idx] is not equal to '(' and s[idx] is not equal to ')'), do −
num := num + s[idx]
(increase idx by 1)
node = new node with value num
if idx < size of s and s[idx] is same as '(', then −
(increase idx by 1)
left of node := solve(s, idx)
(increase idx by 1)
if idx < size of s and s[idx] is same as '(', then −
(increase idx by 1)
right of node := solve(s, idx)
(increase idx by 1)
return node
From the main method do the following −
idx := 0
temp = new node with value -1
return solve(s, idx)
Example (C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class TreeNode{ public: int val; TreeNode *left, *right; TreeNode(int data){ val = data; left = NULL; right = NULL; } }; void inord(TreeNode *root){ if(root != NULL){ inord(root->left); cout << root->val << " "; inord(root->right); } } class Solution { public: TreeNode* solve(string s, int& idx){ if (idx >= s.size()) return NULL; string num = ""; while (idx < s.size() && s[idx] != '(' && s[idx] != ')') { num += s[idx]; idx++; } TreeNode* node = new TreeNode(stoi(num)); if (idx < s.size() && s[idx] == '(') { idx++; node->left = solve(s, idx); idx++; if (idx < s.size() && s[idx] == '(') { idx++; node->right = solve(s, idx); idx++; } } return node; } TreeNode* str2tree(string s) { int idx = 0; TreeNode* temp = new TreeNode(-1); return solve(s, idx); } }; main(){ Solution ob; TreeNode *root = ob.str2tree("4(2(3)(1))(6(5))"); inord(root); }
Input
"4(2(3)(1))(6(5))"
Output
3 2 1 4 5 6
- Related Articles
- Construct String from Binary Tree in Python
- Construct Binary Search Tree from Preorder Traversal in Python
- Construct Binary Tree from Preorder and Inorder Traversal in Python
- Construct Binary Tree from Inorder and Postorder Traversal in Python
- Construct a Binary Search Tree from given postorder in Python
- Construct a Binary Tree from Postorder and Inorder in Python
- Construct Binary Tree from Preorder and Postorder Traversal in Python
- Construct Special Binary Tree from given Inorder traversal in C++
- Construct a complete binary tree from given array in level order fashion in C++
- Construct the derivation tree for the string aabbabba.
- Binary tree to string with brackets in C++
- Construct Tree from given Inorder and Preorder traversals in C++
- Binary Tree to Binary Search Tree Conversion in C++
- Create a mirror tree from the given binary tree in C++ Program
- Difference between Binary Tree and Binary Search Tree
