題目描述
給定一顆二叉搜索樹,請(qǐng)找出其中的第k大的結(jié)點(diǎn)。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按結(jié)點(diǎn)數(shù)值大小順序第三個(gè)結(jié)點(diǎn)的值為4。
public class Solution {
private TreeNode node = null;
private int key = 0;
TreeNode KthNode(TreeNode pRoot, int k) {
if(pRoot == null)
return null;
if(k == 0)
return null;
key = k;
preOrder(pRoot);
return node;
}
private void preOrder(TreeNode root) {
if(root == null)
return ;
preOrder(root.left);
key --;
if(key == 0) {
node = root;
return;
}
preOrder(root.right);
}
}