题解 | #二叉搜索树的第k个结点#python 解法

二叉搜索树的第k个结点

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

其实考的就是中序遍历。 要注意检查pRoot,k(特别是k值)

    def KthNode(self, pRoot, k):
        # write code here
#         按大小顺序输出所有节点的值( 即是中序遍历)
#         idx取出所求的值, 返回。 
#         第0大???要检查k值
        if(pRoot==None or k==0):
            return None
        list_inorder=[]
        def inorder(root):
            if(root ==None):
                return
            inorder(root.left)
            list_inorder.append(root)
            inorder(root.right)
        inorder(pRoot)
        if(k>len(list_inorder)):
            return None
        return (list_inorder[k-1])
全部评论

相关推荐

迷茫的大四🐶:看来已经准备换人了
点赞 评论 收藏
分享
10-17 23:18
已编辑
西北农林科技大学 Web前端
独行m:给25可以试试,但他只能给12,那就是纯纯的事精
秋招,不懂就问
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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