leetcode

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

0257.cpp (722B)


0 class Solution { 1 public: 2 vector<string> binaryTreePaths(TreeNode *root) { 3 if (!root) return {}; 4 5 vector<string> res; 6 stack<pair<TreeNode *, string>> st; 7 st.push({root, to_string(root->val)}); 8 while (!st.empty()) { 9 TreeNode *root = st.top().first; 10 string s = st.top().second; 11 st.pop(); 12 if (!root->left && !root->right) 13 res.push_back(s); 14 else { 15 s += "->"; 16 if (root->left) st.push({root->left, s + to_string(root->left->val)}); 17 if (root->right) st.push({root->right, s + to_string(root->right->val)}); 18 } 19 } 20 return res; 21 } 22 };