平衡二叉树中序遍历(有序)

二叉搜索树的第k个结点

http://www.nowcoder.com/questionTerminal/ef068f602dde4d28aab2b210e859150a

二叉平衡树中序遍历是有序的。
只需要中序遍历就好了

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);
    }
};
全部评论

相关推荐

爱吃烤肠的牛油最喜欢...:50K是ssp了估计,ssp的人家多厉害都不用说,每年比例大概在百分之5左右
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务