K-th node Python binary search tree

Given a binary search tree, find a small node of the k them. For example, (5,3,7,2,4,6,8), sorted by the size of the small value of the third node is node 4

#-*- coding:utf-8 -*-
class TreeNode:
    def __init__(self,x):
        self.val = x
        self.left=None
        self.right =None

class Solution:
    def KthNode(self,pRoot,k):
        retList=[]

        def preOrder(pRoot):
            if pRoot==None:
                return None
            preOrder(pRoot.left)
            retList.append(pRoot)
            preOrder(pRoot.right)

        preOrder(pRoot)

        if len(retList)<k or k <1:
            return None

        return retList[k-1]
Published 135 original articles · won praise 121 · Views 4849

Guess you like

Origin blog.csdn.net/weixin_44208324/article/details/105328086