Share my C++ really easy solution using preorder traversal


  • 0
    C
    class Solution {
    public:
        vector<int> a;
        void t(TreeNode* root)
        {
            if(!root)
            return;
            t(root->left);
            a.push_back(root->val);
            t(root->right);
        }
        int kthSmallest(TreeNode* root, int k) {
            if(k==0)
            return 0;
            if(!root)
            return 0;
            t(root);
            return a[k-1];
            
        }
    
    };
    ==

Log in to reply
 

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