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

int res; 
int k; 
public int kthLargest(TreeNode root, int k) { 
    this.k = k; 
    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) { 
    DFS (node.right); 
    // k == 0 has been found out in advance tree traversal 
    if (k == 0){ 
    // Find the Kth largest node and assign res 
    if (--k == 0){ 
        res = node.val; 

Guess you like

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