【LeetCode从零单刷】Kth Smallest Element in a BST

题目:

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

Note:

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?

解答:

《编程之美》中有讲过类似的题目:寻找数组(未排序)中的第K大数。这里是BST,已经有顺序,更方便一些。

  1. 如果左子树数目大于等于K,那么直接找左子树中第K个;
  2. 如果左子树数目等于 K-1,那么根节点就是第K个;
  3. 如果左子树数目小于K,那么直接找右子树中第(K - leftSum - 1)个(勿忘根节点

关于左子树有多少结点?需要另一个递归程序计算。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int findNodeSum(TreeNode* root) {
        if (root == NULL)       return 0;
        return (findNodeSum(root->left) + findNodeSum(root->right) + 1);
    }

    int kthSmallest(TreeNode* root, int k) {
        int leftSum = findNodeSum(root->left);
        if (leftSum >= k)            return kthSmallest(root->left, k);
        else if (leftSum == k - 1)  return root->val;
        else
        {
            return kthSmallest(root->right,k - leftSum - 1);
        }
    }
};

题目到这儿还没完,如果BST总是在修改呢?

最好应该修改树结点的结构,增加一项属性:左子树的节点总数。这样搜索的复杂度就是 O (height)

版权声明:本文为博主原创文章,转载请联系我的新浪微博 @iamironyoung

时间: 2024-10-21 11:17:41

【LeetCode从零单刷】Kth Smallest Element in a BST的相关文章

[Leetcode] Binary search/tree-230. Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: 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

[LeetCode][JavaScript]Kth Smallest Element in a BST

Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up: What if the BST is modified (insert/delete

【LeetCode】230. Kth Smallest Element in a BST (2 solutions)

Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up:What if the BST is modified (insert/delete

8.3 LeetCode230 Return the kth smallest element of a BST

Kth Smallest Element in a BST Total Accepted: 9992 Total Submissions: 33819My Submissions Question Solution Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k

【LeetCode 230】Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 题意: 给定一个二分搜索树,返回第K小的结点 思路: 只要明白BST树的原理,只要中序遍历一遍BST树即可.求第K小的,只需遍历前K个结点就OK. C++: 1 /*

[LeetCode] 230. Kth Smallest Element in a BST 解题思路

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 问题:找出二叉搜索树种第 k 小的元素. 一个深度遍历的应用.使用递归.或者借助栈都可以实现深度遍历.本文代码使用递归实现. 1 void visit(TreeNod

LeetCode OJ:Kth Smallest Element in a BST(二叉树中第k个最小的元素)

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 求二叉树中第k个最小的元素,中序遍历就可以了,具体代码和另一个Binary Tree Iterator差不多其实,这题由于把=写成了==调bug调了好久,细心细心啊啊

(medium)LeetCode 230.Kth Smallest Element in a BST

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: 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

leetcode 之 Kth Smallest Element in a BST

题目描述: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 给定一棵二叉查找树, 找到第k个小的元素. 思路,利用二叉查找树的特性,其左子树的元素小于根节点,右子树的元素大于根节点.当采用中序遍历时,可以得到从小到