标签:
给定一颗二叉搜索树,请找出其中的第k大的结点。例如,5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
import java.util.ArrayList;
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
TreeNode KthNode(TreeNode pRoot, int k)
{
if (pRoot == null) {
return null;
}
ArrayList<TreeNode> list = new ArrayList<TreeNode>();
myNode(pRoot, k, list);
if (list.size() == 0) {
return null;
} else {
return list.get(0);
}
}
int index = 0;
public void myNode(TreeNode node, int k, ArrayList<TreeNode> list) {
if (node == null) {
return;
}
myNode(node.left, k, list);
index++;
if (index == k) {
list.add(node);
return;
}
myNode(node.right, k, list);
}
// 递归的方法返回结点
public TreeNode myNode01(TreeNode node, int k) {
TreeNode target = null;
if (node.left != null) {
target = myNode01(node.left, k);
}
if (target == null) {
index++;
if (index == k) {
return node;
}
}
if (target == null && node.right != null) {
target = myNode01(node.right, k);
}
return target;
}
}
标签:
原文地址:http://www.cnblogs.com/rosending/p/5722058.html