简体   繁体   English

在二叉搜索树中找到第 k 个最小的元素

[英]Finding the kth smallest element in the binary search tree

I was trying to solve this problem from leetcode and the prompt looks like this:我试图从 leetcode 解决这个问题,提示如下:

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.给定一个二叉搜索树,写一个 function kthSmallest 来找到其中的第 k 个最小元素。 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/

class Solution {
   public int kthSmallest(TreeNode root, int k) {
       TreeNode curr = new TreeNode(0);
       ArrayList<TreeNode> res = new ArrayList<TreeNode>();
       res = inOrder(root);
       if(res != null){
           curr = res.get(k);
           return curr.val;
       }
       return -1; //if not found
   }
   
   public ArrayList<TreeNode> inOrder(TreeNode root){ //the value of the nodes would be in increasing order 
       ArrayList<TreeNode> list = new ArrayList<TreeNode>();
       if(root == null){
           return list;
       }
       list.addAll(inOrder(root.left));
       list.addAll(inOrder(root));
       list.addAll(inOrder(root.right));
       return list;
   }
}

However, the system gave me the "memory limit exceeded" error message, is my logic faulted or is there anyway I could fix my code?但是,系统给了我“超出内存限制”错误消息,是我的逻辑有问题还是我可以修复我的代码? Thanks in advance!提前致谢!

Your logic is probably fine, however should be an efficiency issue since you're getting MLE.您的逻辑可能很好,但是由于您获得了 MLE,因此应该是一个效率问题。 It seems you're using two extra spaces, which we won't need that for solving this problem.您似乎使用了两个额外的空格,我们不需要它来解决这个问题。

This'll pass in Java:这将传入 Java:

public final class Solution {
    private static int res = 0;
    private static int count = 0;

    public final int kthSmallest(
        final TreeNode root, 
        final int k
    ) {
        count = k;
        inorder(root);
        return res;
    }

    private final void inorder(
        final TreeNode node
    ) {
        if (node.left != null)
            inorder(node.left);
        count--;
        if (count == 0) {
            res = node.val;
            return;
        }
        if (node.right != null)
            inorder(node.right);
    }
}

and here is a Python version, if you'd be interested, similarly with inorder traversal:这是一个 Python 版本,如果您有兴趣,与中inorder遍历类似:

class Solution:
    def kthSmallest(self, root, k):
        def inorder(node):
            if not node:
                return
            inorder(node.left)
            self.k -= 1
            if self.k == 0:
                self.res = node.val
                return
            inorder(node.right)

        self.k, self.res = k, None
        inorder(root)
        return self.res

References参考

  • For additional details, please see the Discussion Board where you can find plenty of well-explained accepted solutions with a variety of languages including low-complexity algorithms and asymptotic runtime / memory analysis 1 , 2 .有关更多详细信息,请参阅讨论板,您可以在其中找到大量解释清楚且公认的解决方案,这些解决方案具有多种语言,包括低复杂度算法和渐近运行时/ memory分析12

  • Brute force algorithms usually get accepted for easy questions.对于简单的问题,蛮力算法通常会被接受。 For medium and hard questions, brute force algorithms mostly fail with Time Limit Exceeded (TLE) and less with Memory Limit Exceeded (MLE) errors.对于中等难度和难度的问题,蛮力算法大多会因超出时间限制(TLE) 而失败,而较少因 Memory 超出限制 (MLE) 错误而失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM