其实考的就是中序遍历。 要注意检查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])
京公网安备 11010502036488号