剑指offer面试题54(java版):二叉搜索树的第K小节点

welcome to my blog

剑指offer面试题54(java版):二叉搜索树的第K小节点

题目描述

给定一棵二叉搜索树,请找出其中的第k小的结点。例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4

第一次做; 力扣上把这道题改成: 请找出其中的第k大的结点; 中序遍历; 迭代版, 右根左的遍历形式
class Solution {
    public int kthLargest(TreeNode root, int k) {
        Stack<TreeNode> stack = new Stack<>();
        while(!stack.isEmpty() || root!=null){
            if(root!=null){
                stack.push(root);
                root = root.right;
            }
            else{
                root = stack.pop();
                if(--k == 0)
                    return root.val;
                root = root.left;
            }
        }
        //不会执行到这里
        return -1;
    }
}
第一次做; 力扣上把这道题改成: 请找出其中的第k大的结点; 核心: 右根左的遍历方式
class Solution {
    private int i=0;
    public int kthLargest(TreeNode root, int k) {
        if(root==null){
            return -1;
        }
        int res = kthLargest(root.right, k);
        if(res!=-1){
            return res;
        }
        i++;
        if(i==k){
            return root.val;
        }
        return kthLargest(root.left, k);
    }
}

思路

  • 见注释

笔记

  • 一定要弄清楚所使用的的递归函数的逻辑
  • 本题使用的递归函数的逻辑: 针对当前节点p,
    • 先判断p的左子节点是否为第k小节点,是的话返回p的左子节点;
    • 如果p的左子节点不满足条件再判断当前节点是否满足条件;
    • 如果当前节点不满足条件则再判断当前节点的右子节点是否满足条件
第三次做; 创建全局变量作为索引; base case中的k<1比第二次做考虑的稍微全面一点
public class Solution {
    int index=0;
    TreeNode KthNode(TreeNode pRoot, int k)
    {
        if(pRoot==null || k < 1)
            return null;
        TreeNode res = KthNode(pRoot.left, k);
        if(res!=null)
            return res;
        index++;//空节点不参与计数
        if(index==k)
            return pRoot;
        return KthNode(pRoot.right, k);
    }
}
第二次做, 需要一个全局变量统计遍历的次数; 感受一下处理逻辑…估计之后又忘了
public class Solution {
    int count = 0;
    TreeNode KthNode(TreeNode pRoot, int k)
    {
        //base case
        if(pRoot==null)
            return null;
        //
        TreeNode curr;
        curr = KthNode(pRoot.left, k);
        if(curr!=null)
            return curr;
        count++;
        if(count==k)
            return pRoot;
        return KthNode(pRoot.right, k);
    }
}
中序遍历(使用一个全局变量)
  • 递归的逻辑是:判断当前节点是否是第k小节点,是的话就返回当前节点; 不过在此之前得先判断当前节点的左子节点是否是第k小节点
public class Solution {
    TreeNode KthNode(TreeNode pRoot, int k)
    {
        /*
        二叉搜索树的中序遍历结果是递增序列
        所以中序遍历的第k个结果就是第k小节点
        这道题还能换个问法:求第k大节点
        只需把左根右的遍历方式改成右根左的遍历方式即可
        */
        //input check
        if(pRoot==null||k==0)
            return null;
        //execute
        count = k;
        TreeNode res = Core(pRoot);
        return res;
    }
    public int count;
    public TreeNode Core(TreeNode p){
        //中序遍历:左根右
        if(p==null)
            return null;
        TreeNode curr;
        curr = Core(p.left);
        if(--count==0)
            curr = p;
        if(count > 0)
            curr = Core(p.right);
        return curr;
    }
}
/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值