题解 | #二叉搜索树的第k个结点#
二叉搜索树的第k个结点
http://www.nowcoder.com/practice/ef068f602dde4d28aab2b210e859150a
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def __init__(self):
self.count = 0
# 返回对应节点TreeNode
def KthNode(self, pRoot, k):
# write code here
if pRoot is None:
return None
ret = self.KthNode(pRoot.left, k)
if ret is not None:
return ret
self.count += 1
# print(k, self.count, pRoot.val)
if self.count == k:
return pRoot
ret = self.KthNode(pRoot.right, k)
if ret is not None:
return ret