题解 | #二叉搜索树的第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])
全部评论

相关推荐

06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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