share recursive c++ solution, without change the structure


  • 0
    N
    class Solution {
    public:
        int kth(TreeNode* root, int k, int& n){
            int val = 0;
            if(root->left) val = kth(root->left, k, n);
            if(n == k) return val;
            if(++n == k) return root->val;
            if(root->right) val  = kth(root->right, k, n);
            return val;
        }
        int kthSmallest(TreeNode* root, int k) {
            if(NULL == root) return 0;
            int n = 0;
            return kth(root, k, n);
        }
    };
    

Log in to reply
 

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