10 lines concise and easy understand c++ solution, iterative + stack


  • 0
    A
    class Solution {
    public:
        int kthSmallest(TreeNode* root, int k) {
            stack<TreeNode*> st;
            TreeNode* it = root;
            while(it || !st.empty()){
                while(it){
                    st.push(it);
                    it = it->left;
                }
                it = st.top()->right;
                if(--k == 0) return st.top()->val;
                st.pop();
            }
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.