leetcode

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

0230.cpp (416B)


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