给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。
思路:
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回对应节点TreeNode
def KthNode(self, pRoot, k):#取第k个
# write code here
global result
result=[]
self.midnode(pRoot)
if k<=0 or len(result)<k:
return None
else:
return result[k-1]
def midnode(self,root):#中序遍历
if not root:
return None
self.midnode(root.left)
result.append(root)
self.midnode(root.right)