首发于 语雀文档@blueju
代码:
/*** Definition for a binary tree node.* class TreeNode {* val: number* left: TreeNode | null* right: TreeNode | null* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {* this.val = (val===undefined ? 0 : val)* this.left = (left===undefined ? null : left)* this.right = (right===undefined ? null : right)* }* }*/function kthLargest(root, k) {const treeNodeList = []const check = (root) => {if (root === null)returntreeNodeList.push(root.val)check(root.left)check(root.right)}check(root)const afterSort = treeNodeList.sort((a, b) => b - a)return afterSort[k - 1]};
