您的位置:首页 > Web前端

剑指Offer--二叉搜索树的第K个节点-不会

2017-08-19 16:26 246 查看
题目描述
给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。

public class Solution {
public int count = 0;
TreeNode KthNode(TreeNode pRoot, int k)
{
if(pRoot != null){ //中序遍历寻找第k个
TreeNode node = KthNode(pRoot.left,k);
if(node != null)
return node;
count ++;
if(count == k)
return pRoot;
node = KthNode(pRoot.right,k);
if(node != null)
return node;
}
return null;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: