鱼C论坛

 找回密码
 立即注册
查看: 868|回复: 0

[学习笔记] leetcode 230. Kth Smallest Element in a BST

[复制链接]
发表于 2019-9-22 09:29:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

  2. Note:
  3. You may assume k is always valid, 1 ≤ k ≤ BST's total elements.

  4. Example 1:

  5. Input: root = [3,1,4,null,2], k = 1
  6.    3
  7.   / \
  8. 1   4
  9.   \
  10.    2
  11. Output: 1
  12. Example 2:

  13. Input: root = [5,3,6,2,4,null,null,1], k = 3
  14.        5
  15.       / \
  16.      3   6
  17.     / \
  18.    2   4
  19.   /
  20. 1
  21. Output: 3
  22. Follow up:
  23. 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?
复制代码

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. *     int val;
  5. *     TreeNode left;
  6. *     TreeNode right;
  7. *     TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11.     public int kthSmallest(TreeNode root, int k) {
  12.         List<Integer> list = new ArrayList<>();
  13.         if(root == null) return 0;
  14.         help(root,list);
  15.         Integer[] re = new Integer[list.size()];
  16.         list.toArray(re);
  17.         Arrays.sort(re);
  18.         
  19.         return re[k-1];
  20.     }
  21.     public void help(TreeNode root, List<Integer> list){
  22.         if(root == null) return;
  23.         if(root != null){
  24.             help(root.left,list);
  25.             list.add(root.val);
  26.             help(root.right,list);
  27.         }
  28.     }
  29. }
复制代码

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. *     int val;
  5. *     TreeNode left;
  6. *     TreeNode right;
  7. *     TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11.     public int kthSmallest(TreeNode root, int k) {
  12.         List<Integer> list = new ArrayList<>();
  13.         if(root == null) return 0;
  14.         help(root,list);
  15.         Integer[] re = new Integer[list.size()+1];
  16.         
  17.         for(int i = 0; i < list.size(); i++){
  18.             re[list.get(i)] = 0;
  19.             re[list.get(i)]++;
  20.         }
  21.         for(int i = 0; i< re.length; i++){
  22.             if(re[i] == null) continue;
  23.             if(re[i] == 1) k--;
  24.             if(k == 0) return i;
  25.         }
  26.         return 0;
  27.     }
  28.     public void help(TreeNode root, List<Integer> list){
  29.         if(root == null) return;
  30.         if(root != null){
  31.             help(root.left,list);
  32.             list.add(root.val);
  33.             help(root.right,list);
  34.         }
  35.     }
  36. }
复制代码

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. *     int val;
  5. *     TreeNode left;
  6. *     TreeNode right;
  7. *     TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11.     public int kthSmallest(TreeNode root, int k) {
  12.         List<Integer> list = new ArrayList<>();
  13.         if(root == null) return 0;
  14.         help(root,list);
  15.         
  16.         return list.get(k-1);
  17.     }
  18.     public void help(TreeNode root, List<Integer> list){
  19.         if(root == null) return;
  20.         if(root != null){
  21.             help(root.left,list);
  22.             list.add(root.val);
  23.             help(root.right,list);
  24.         }
  25.     }
  26. }
复制代码

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. *     int val;
  5. *     TreeNode left;
  6. *     TreeNode right;
  7. *     TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11.     int kth = 0;
  12.     int result = 0;
  13.     public int kthSmallest(TreeNode root, int k) {
  14.         kth = k;
  15.         help(root);
  16.         return result;
  17.         
  18.     }
  19.     private void help(TreeNode root){
  20.         if(root == null || kth == 0) return;
  21.         
  22.         help(root.left);
  23.         kth--;
  24.         if(kth == 0) result = root.val;
  25.         help(root.right);
  26.     }
  27. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 22:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表