题解 | #二叉搜索树的第k个节点#
二叉搜索树的第k个节点
http://www.nowcoder.com/practice/57aa0bab91884a10b5136ca2c087f8ff
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param proot TreeNode类 # @param k int整型 # @return int整型 # class Solution: def KthNode(self , proot: TreeNode, k: int) -> int: # write code here rst = [] if not proot or k == 0: return -1 def dfs(root): if len(rst) > k or not root: return dfs(root.left) rst.append(root.val) dfs(root.right) dfs(proot) if k > len(rst): return -1 return rst[k - 1]