leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0144.cpp (491B)
0 class Solution { 1 public: 2 vector<int> preorderTraversal(TreeNode *root) { 3 if (!root) return {}; 4 5 vector<int> res; 6 stack<TreeNode *> st; 7 st.push(root); 8 while (!st.empty()) { 9 TreeNode *root = st.top(); 10 st.pop(); 11 while (root) { 12 res.push_back(root->val); 13 if (root->right) st.push(root->right); 14 root = root->left; 15 } 16 } 17 return res; 18 } 19 };