leetcode

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

144.cpp (392B)


      1 class Solution {
      2 public:
      3   vector<int> preorderTraversal(TreeNode *root) {
      4     if (!root) return {};
      5 
      6     vector<int> res;
      7     stack<TreeNode *> st;
      8     st.push(root);
      9     while (!st.empty()) {
     10       TreeNode *root = st.top();
     11       st.pop();
     12       res.push_back(root->val);
     13       if (root->right) st.push(root->right);
     14       if (root->left) st.push(root->left);
     15     }
     16     return res;
     17   }
     18 };