Offer_day15_54. 二叉搜索树的第k大节点
给定一棵二叉搜索树,请找出其中第k大的节点。
示例 1:
输入: root = [3,1,4,null,2], k = 1
3/ \1 4\2
输出: 4
示例 2:
输入: root = [5,3,6,2,4,null,null,1], k = 3
5/ \3 6/ \2 4/1
输出: 4
限制:
1 ≤ k ≤ 二叉搜索树元素个数
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
代码:
import time
from typing import Listclass TreeNode:def __init__(self, x):self.val = xself.left = Noneself.right = Noneclass Solution:def __init__(self):self.middle_array = []def middle(self,root:TreeNode):if root is None:returnself.middle(root.left)self.middle_array.append(root.val)self.middle(root.right)def kthLargest(self, root: TreeNode, k: int) -> int:self.middle(root)print(self.middle_array)return self.middle_array[-k]def test(data_test):s = Solution()return s.getResult(*data_test)def test_obj(data_test):result = [None]obj = Solution(*data_test[1][0])for fun, data in zip(data_test[0][1::], data_test[1][1::]):if data:res = obj.__getattribute__(fun)(*data)else:res = obj.__getattribute__(fun)()result.append(res)return resultif __name__ == '__main__':datas = [[],]for data_test in datas:t0 = time.time()print('-' * 50)print('input:', data_test)print('output:', test(data_test))print(f'use time:{
time.time() - t0}s')
备注:
GitHub:https://github.com/monijuan/leetcode_python
CSDN汇总:模拟卷Leetcode 题解汇总_卷子的博客-CSDN博客
可以加QQ群交流:1092754609
leetcode_python.utils详见汇总页说明
先刷的题,之后用脚本生成的blog,如果有错请留言,我看到了会修改的!谢谢!