剑指 Offer 54. 二叉搜索树的第k大节点

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    int res,k;
    public int kthLargest(TreeNode root, int k) {
        this.k = k;
        dfs(root);
        return res;
    }

    void dfs(TreeNode root){
        if(root == null) return;
        dfs(root.right);
        if(--k==0){
            res = root.val;
            return;
        }

        dfs(root.left);

    }
}
上一篇:2021-2022学年英语周报七年级第54期答案及试题


下一篇:48.Mysql中的checkpoint机制