二叉搜索树的第k个结点
class Solution {
public:
TreeNode* res=NULL;
void mid(TreeNode* root, int k,int &cnt)
{
if(!root)
return;
mid(root->left,k,cnt);
cnt++;
if(cnt==k) res=root;
mid(root->right,k,cnt);
}
TreeNode* kthNode(TreeNode* root, int k) {
int cnt=0;
mid(root,k,cnt);
return res;
}
};
有帮助的话可以点个赞,我会很开心的~