230. Kth Smallest Element in a BST

题目

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

我的想法

对于查找树来说,左子树<root<右子树,因此用中序遍历BST即可按照升序访问节点
递归,利用Integer.MAX_VALUE - root.val来传递找到的第k小数值

class Solution {
    public int kthSmallest(TreeNode root, int k) {
        return Integer.MAX_VALUE - inorder(root, 0, k);
    }
    public int inorder(TreeNode root, int count, int k){
        if(root == null) return count;
        int res = inorder(root.left, count, k);
        if(res > Integer.MAX_VALUE - 10000) return res;
        if(++res == k) return Integer.MAX_VALUE - root.val; //找到了第k小
        res = inorder(root.right, res, k);
        return res;
    }
}

迭代法

class Solution {
    public int kthSmallest(TreeNode root, int k) {
        int count = 0;
        Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
        while(!stack.isEmpty() || root != null){
            while(root != null){
                stack.push(root);
                root = root.left;
            } 
            root = stack.pop();
            if(++count == k) break; //如果找到第k小,跳出循环
            root = root.right;
        }
        return root.val;
    }
}

Follow Up Solution
没看懂意思,是要优化BST的结构?

2019.10.21 update:
这两种方法本质上是一个方法,即利用BST的升序进行中序遍历,不过实现方法不一样,一个迭代一个递归。这里迭代更新了中序遍历的迭代模板。每个结点只会进出stack一次,时间为复杂度O(h + k), +号可以理解为max(h, k),h为数的高度。对于follow up,可以采用quick select的思想,把每个结点及其子结点数记录在一个map中,根据map进行二分查询

//迭代
public class Solution {
    public int kthSmallest(TreeNode root, int k) {
        Deque<TreeNode> stack = new ArrayDeque<>();
        while(root != null) {
            stack.push(root);
            root = root.left;
        }
        //注意!!!这里不能写成stack==null
        while(!stack.isEmpty()) {
            TreeNode cur = stack.pop();
            k--;
            if(k == 0) {
                return cur.val;
            }
            if(cur.right != null) {
                cur = cur.right;
                while(cur != null) {
                    stack.push(cur);
                    cur = cur.left;
                }
            }
        }
        return -1;
    }
}
//递归
public class Solution {
    int count;
    public int kthSmallest(TreeNode root, int k) {
        count = k;
        TreeNode node = helper(root);
        return node.val;
    }
    private TreeNode helper(TreeNode root) {
        if(root == null || count < 0) {
            return null;
        }
        
        TreeNode left = helper(root.left);
        if(left != null) {
            return left;
        }
        if(--count == 0) {
            return root;
        }
        TreeNode right = helper(root.right);
        if(right != null) {
            return right;
        }
        return null;
    }
}
//quick select
public class Solution {
    public int kthSmallest(TreeNode root, int k) {
        Map<TreeNode, Integer> numOfChildren = new HashMap<>();
        countNodes(root, numOfChildren);
        return quickSelectOnTree(root, k, numOfChildren);
    }
    
    private int countNodes(TreeNode root, Map<TreeNode, Integer> numOfChildren) {
        if (root == null) {
            return 0;
        }
        
        int left = countNodes(root.left, numOfChildren);
        int right = countNodes(root.right, numOfChildren);
        numOfChildren.put(root, left + right + 1);
        return left + right + 1;
    }
    
    private int quickSelectOnTree(TreeNode root, int k, Map<TreeNode, Integer> numOfChildren) {
        if (root == null) {
            return -1;
        }
        
        int left = root.left == null ? 0 : numOfChildren.get(root.left);
        if (left >= k) {
            return quickSelectOnTree(root.left, k, numOfChildren);
        }
        if (left + 1 == k) {
            return root.val;
        }
        
        return quickSelectOnTree(root.right, k - left - 1, numOfChildren);
    }
}

解答

leetcode solution 1: Recursion
按照中序遍历的顺序,把数值存入数组,再获取数组中第k-1个数即为所求。不过有对数组的操作速度没有我的好
疑问:这里额外定义了一个ArrayList,为什么占用空间比我的方法还要小一些?

class Solution {
  public ArrayList<Integer> inorder(TreeNode root, ArrayList<Integer> arr) {
    if (root == null) return arr;
    inorder(root.left, arr);
    arr.add(root.val);            //把节点数组按中序顺序存入数组
    inorder(root.right, arr);
    return arr;
  }

  public int kthSmallest(TreeNode root, int k) {
    ArrayList<Integer> nums = inorder(root, new ArrayList<Integer>());
    return nums.get(k - 1);
  }
}

Follow Up Solution
意思大概是,这个BST树经常要进行增删操作,问在这种情况下怎样优化kth查找
解答:
把遍历后的结果存在一个双向链表中,BST改变后对链表进行相应的更新

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值