Very simple solution for inorder traverse


  • 0
    T
    int kthSmallest(TreeNode* root, int k) {
            vector<TreeNode*> v;
            vector<int> vec;
            TreeNode *p = root;
            while(vec.size() < k || p){
                if(p){
                    v.push_back(p);
                    p = p->left;
                }
                else{
                    p = v.back();
                    v.pop_back();
                    vec.push_back(p->val);
                    p = p->right;
                } 
                
            }
            return vec.back();
        }

Log in to reply
 

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