Straightforward recursive C solution


  • 0
    S
    int inorder[9999];
    int size;
    
    void traverse(struct TreeNode* node); 
    int kthSmallest(struct TreeNode* root, int k) {
    size = 0;
    traverse(root);
    return inorder[k - 1];
    }
    
    void traverse(struct TreeNode* node){
    
    if(node->left != NULL) traverse(node->left);
    inorder[size++] = node->val;
    if(node->right != NULL) traverse(node->right);
    }

Log in to reply
 

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