目录
题目描述
给定一棵二叉搜索树,请找出其中的第k小的结点。例如,在下图的二叉搜索树里,按节点数值大小顺序,第三大节点的值是4。
测试用例
- 功能测试(各种形态不同的二叉搜索树)
- 边界值测试(输入k为0、1、二叉搜索树的节点数、二叉树搜索树的节点数+1)
- 特殊输入测试(指向二叉搜索树根节点的指针为空指针)
题目考点
- 考察应聘者的只是迁移能力,利用中序遍历解题。
- 考察应聘者对二叉搜索树和中序遍历的特点的理解。
解题思路
按照中序遍历的顺序遍历一个二叉搜索树。
只要熟悉中序遍历的写法,那么这道题就不难了。
参考解题
/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
// 中序遍历
private int k;
TreeNode KthNode(TreeNode pRoot, int k)
{ // 异常
if(pRoot == null || k == 0){
return null;
}
this.k = k;
return KthNodeCore(pRoot);
}
// 中序遍历第k个元素
public TreeNode KthNodeCore(TreeNode pRoot){
TreeNode target = null;
if(pRoot.left != null){
target = KthNodeCore(pRoot.left);
}
if(target == null){
// 走到最左边?
if(k == 1){
target = pRoot;
}
k--;
}
if(target == null && pRoot.right != null){
target = KthNodeCore(pRoot.right);
}
return target;
}
}
中序遍历
class TreeNode{
public int data;
public TreeNode leftChild;
public TreeNode rightChild;
public static void inOrderTraversal(TreeNode node){
if(node == null){
return;
}else{
inOrderTraversal(node.leftChild);
System.out.println(node.data);
inOrderTRaversal(node.rightChild);
}
}
}