题解 | #二叉搜索树的第k个节点#
二叉搜索树的第k个节点
http://www.nowcoder.com/practice/57aa0bab91884a10b5136ca2c087f8ff
class Solution {
public:
TreeNode *res = NULL;
void midOrder(TreeNode *proot, int &k) {
if(!proot || k < 0) return;
midOrder(proot->left, k);
k--;
if(k == 0) {
res = proot;
res->left = NULL;
res->right =NULL;
}
midOrder(proot->right, k);
}
TreeNode* KthNode(TreeNode* proot, int k) {
// write code here
midOrder(proot, k);
return res;
}
};