# topic:

Given a binary search tree, please find out the k-th largest nodes.

# analysis:

Inorder traversal results binary search tree is a sequence ordered from small to large, the K-looking large nodes may be ordered into a sequence in descending, order of use is left Une recursive traversal, while using a counter, the time when the counter reaches k, the k is a large nodes.

# solution:

```
package com.wsy;
class Tree {
private int value;
private Tree left;
private Tree right;
public Tree() {
}
public Tree(int value) {
this.value = value;
this.left = this.right = null;
}
public Tree(int value, Tree left, Tree right) {
this.value = value;
this.left = left;
this.right = right;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public Tree getLeft() {
return left;
}
public void setLeft(Tree left) {
this.left = left;
}
public Tree getRight() {
return right;
}
public void setRight(Tree right) {
this.right = right;
}
}
public class Main {
public static int count;
public static void main(String[] args) {
Tree tree = init();
int k = 3;
getKth(tree, k);
}
public static Tree init() {
Tree tree8 = new Tree(8);
Tree tree6 = new Tree(6);
Tree tree4 = new Tree(4);
Tree tree2 = new Tree(2);
Tree tree7 = new Tree(7, tree6, tree8);
Tree tree3 = new Tree(3, tree2, tree4);
Tree tree5 = new Tree(5, tree3, tree7);
return tree5;
}
public static void getKth(Tree tree, int k) {
if (tree == null || k < 1) {
return;
}
if (tree.getRight() != null) {
getKth(tree.getRight(), k);
}
count++;
if (count == k) {
System.out.println("第" + k + "大的结点是：" + tree.getValue());
}
if (tree.getLeft() != null) {
getKth(tree.getLeft(), k);
}
}
}
```