二叉平衡树中序遍历是有序的。
只需要中序遍历就好了
class Solution {
public:
int index;
TreeNode* root=NULL;
TreeNode* KthNode(TreeNode* pRoot, int k)
{
//中序遍历,第k个
index=k;
inOrder(pRoot);
return root;
}
void inOrder(TreeNode *pRoot)
{
if(pRoot==NULL)
return ;
inOrder(pRoot->left);
index--;
if(index==0){
root=pRoot;
return ;
}
inOrder(pRoot->right);
}
};


京公网安备 11010502036488号