题目描述:给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
思路:设置一个cnt,记录第k小的值,先遍历左子节点,若节点的左子节点为空则cnt加1,递归到左子树,否则在当前节点cnt加1,若当前计数小于k则遍历右子树。
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回对应节点TreeNode
def __init__(self):