leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0606.cpp (722B)


0 class Solution {
1 public:
2 string tree2str(const TreeNode *root) {
3 stack<const TreeNode *> stack;
4 stack.push(root);
5 string res = "";
7 while (!stack.empty()) {
8 const TreeNode *temp = stack.top();
9 stack.pop();
10 if (temp == nullptr)
11 res += ')';
12 else {
13 res += '(' + to_string(temp->val);
14 stack.push(nullptr);
16 if (temp->right) stack.push(temp->right);
18 if (temp->left)
19 stack.push(temp->left);
20 else if (temp->right)
21 res += "()";
22 }
23 }
24 return res.substr(1, res.size() - 2);
25 }
26 };