很简单的中序遍历,代码如下:
//中序遍历
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
DFS(root, k);
return res;
}
void DFS(TreeNode* root, int k) {
if (root == NULL)
return;
DFS(root->left, k);
num++;
if (k == num)
{
res = root->val;
return;
}
DFS(root->right, k);
}
private:
int res = 0;
int num = 0;
};