难度简单188
给定一棵二叉搜索树,请找出其中第k大的节点。
示例 1:
输入: root = [3,1,4,null,2], k = 1 3 / \ 1 4 \ 2 输出: 4
#右》根》左
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def kthLargest(self, root: TreeNode, k: int) -> int:
self.num=0
def findk(cur):
if not cur:
return
right=findk(cur.right)
if right:
return right
self.num+=1
if k==self.num:
return cur.val
left=findk(cur.left)
if left:
return left
return findk(root)