int res; int k; public int kthLargest(TreeNode root, int k) { this.k = k; dfs(root); return res; } public void dfs(TreeNode node){ // take right -> root -> left way through the tree to give a large reverse easy to find the k-th node IF (node == null) { return; } DFS (node.right); // k == 0 has been found out in advance tree traversal if (k == 0){ return; } // Find the Kth largest node and assign res if (--k == 0){ res = node.val; } dfs(node.left); }

## Sword refers to the k-th largest node of the Offer-55 binary search tree

### Guess you like

Origin blog.csdn.net/a792396951/article/details/114255257

Recommended

Ranking