剑指 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 {

    public int num = 0, ans = 0;

    public int kthLargest(TreeNode root, int k) {
        InOrder(root, k);
        return ans;
    }

    public void InOrder(TreeNode root, int k) {
        if(root == null)    return ;
        InOrder(root.right, k);
        if(++num == k) {
            ans = root.val;
            return ;
        }
        InOrder(root.left, k);
    }
}
原文地址:https://www.cnblogs.com/mjn1/p/14282898.html