中序遍历
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param proot TreeNode类
* @param k int整型
* @return int整型
*/
int KthNode(TreeNode* proot, int k) {
if (proot == nullptr) {
return -1;
}
int count = 0;
int res = -1;
curision(proot, k, count, res);
return res;
}
private:
void curision(TreeNode *root, const int &k, int &count, int &res) {
if (root == nullptr) {
return ;
}
curision(root->left, k, count, res);
++count;
if (count == k) {
res = root->val;
return ;
}
curision(root->right, k, count, res);
}
};