-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0257.cpp
More file actions
23 lines (22 loc) · 722 Bytes
/
Copy path0257.cpp
File metadata and controls
23 lines (22 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<string> binaryTreePaths(TreeNode *root) {
if (!root) return {};
vector<string> res;
stack<pair<TreeNode *, string>> st;
st.push({root, to_string(root->val)});
while (!st.empty()) {
TreeNode *root = st.top().first;
string s = st.top().second;
st.pop();
if (!root->left && !root->right)
res.push_back(s);
else {
s += "->";
if (root->left) st.push({root->left, s + to_string(root->left->val)});
if (root->right) st.push({root->right, s + to_string(root->right->val)});
}
}
return res;
}
};