/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
private:
//記錄是否周遊到第k個元素了
int count;
//以pRoot為根的中序周遊
//此處一定要是指針的引用,不然傳回的是空
void inorder(TreeNode* pRoot,int k,TreeNode*&res)
{
if(pRoot==NULL) return;
inorder(pRoot->left,k,res);
count++;
if(count==k)
{
res=pRoot;
//一旦滿足,逐一跳回
return;
}
inorder(pRoot->right,k,res);
}
public:
TreeNode* KthNode(TreeNode* pRoot, int k)
{
//初始化
count=0;
TreeNode* res=nullptr;
inorder(pRoot,k,res);
return res;
}
};