binary_tree_from_string_with_bracket_representation namespace
Construct Binary Tree from String with bracket representation Construct a binary tree from a string consisting of parenthesis and integers. The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the roots value and a pair of parenthesis contains a child binary tree with the same structure. Always start to construct the left child node of the parent first if it exists.
Example 1:
Input: "1(2)(3)" Output: 2 1 3 Explanation: 1 / \ 2 3 Explanation: first pair of parenthesis contains left subtree and second one contains the right subtree. Inorder of above tree is "2 1 3". Example 2:
Input: "4(2(3)(1))(6(5))" Output: 3 2 1 4 5 6 Explanation: 4 / \ 2 6 / \ / 3 1 5
Example 1:
Input: "1(2)(3)" Output: 2 1 3 Explanation: 1 / \ 2 3 Explanation: first pair of parenthesis contains left subtree and second one contains the right subtree. Inorder of above tree is "2 1 3". Example 2:
Input: "4(2(3)(1))(6(5))" Output: 3 2 1 4 5 6 Explanation: 4 / \ 2 6 / \ / 3 1 5
Classes
Functions
- auto treeFromString(std::string str) -> Node*
- auto treeFromStringRec(std::string& str, unsigned long& i) -> Node*
Function documentation
Node* binary_ tree_ from_ string_ with_ bracket_ representation:: treeFromString(std::string str)
Node* binary_ tree_ from_ string_ with_ bracket_ representation:: treeFromStringRec(std::string& str,
unsigned long& i)
Parameters | |
---|---|
str | |
i | |
Returns | Node* |