Skip to content

Commit 3b106aa

Browse files
committed
Prob:183 String from tree
1 parent 1c59665 commit 3b106aa

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 182 |
9+
| Total Problems | 183 |
1010

1111
</center>
1212

@@ -132,6 +132,7 @@ Include contains single header implementation of data structures and some algori
132132
| Validate if a given binary tree is a binary search tree. | [validate_bst.cpp](tree_problems/validate_bst.cpp) |
133133
| Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.| [find_target_k.cpp](tree_problems/find_target_k.cpp) |
134134
| Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Also, to note that the target value is a floating point. There will be only one unique value which is closest to the target. |[closest_bst_value.cpp](tree_problems/closest_bst_value.cpp) |
135+
| Given a binary tree, traversing preorder, construct a string output containing node values and parenthesis. The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree. Examples in code file| [string_from_tree.cpp](tree_problems/string_from_tree.cpp)|
135136

136137
### String Problems
137138
| Problem | Solution |

Diff for: tree_problems/string_from_tree.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Given a binary tree, traversing preorder, construct a string output
3+
* containing node values and parenthesis.
4+
* The null node needs to be represented by empty parenthesis pair "()".
5+
* And you need to omit all the empty parenthesis pairs that don't affect
6+
* the one-to-one mapping relationship between the string and the original
7+
* binary tree
8+
*
9+
* Example:
10+
* 1
11+
* / \
12+
* 2 3
13+
* / \
14+
* 4 5
15+
*
16+
* Output: 1(2(4)(5))(3)
17+
* Originally it should be 1(2(4)(5))(3()()), but we are ommiting
18+
* all the unnecessary empty parenthesis pairs.
19+
*
20+
* Example:
21+
*
22+
* 1
23+
* / \
24+
* 2 3
25+
* \
26+
* 4
27+
* Output: 1(2()(4))(3)
28+
* Here, we can't omit the first parenthesis pair to break
29+
* the one-to-one mapping relationship between the input and the output.
30+
* As without empty parenthesis, it would not be clear that 4 is a right child.
31+
*/
32+
33+
#include <iostream>
34+
35+
struct TreeNode {
36+
int data;
37+
TreeNode* left;
38+
TreeNode* right;
39+
TreeNode(int d):
40+
data{d}, left{nullptr}, right{nullptr}{}
41+
};
42+
43+
44+
std::string string_from_tree(TreeNode* root)
45+
{
46+
if (root == nullptr) {
47+
return std::string("");
48+
}
49+
50+
if (root->left == nullptr && root->right == nullptr) {
51+
return std::to_string(root->data);
52+
}
53+
54+
if (root->right == nullptr) {
55+
return std::to_string(root->data) + "(" +
56+
string_from_tree(root->left) +")";
57+
}
58+
59+
return std::to_string(root->data) + "(" +
60+
string_from_tree(root->left) + ")(" +
61+
string_from_tree(root->right) + ")";
62+
63+
}
64+
65+
int main()
66+
{
67+
TreeNode* root = new TreeNode(1);
68+
root->left = new TreeNode(2);
69+
root->right = new TreeNode(3);
70+
root->left->right = new TreeNode(4);
71+
std::cout << "Tree to string: " << string_from_tree(root)
72+
<< std::endl;
73+
return 0;
74+
}

0 commit comments

Comments
 (0)