Simple C++ solution


  • 0
    S
    class Solution {
    public:
    int visit(TreeNode* root, int &k){
    	if(!root)	return 0;
    	int res = visit(root->left,k);
    	if(k == 0)	return res;
    	if(--k == 0)	return root->val;
    	return visit(root->right,k);
    }
    
    int kthSmallest(TreeNode* root, int k) {
        return visit(root,k);
    }
    };

Log in to reply
 

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