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?

Hint:

Try to utilize the property of a BST.Show More Hint

Credits:

Special thanks to @ts for adding this problem and creating all test cases.

两种思路:

1.空间换时间

BST的特性是,如果按照中序排列,得到的递增序;所以可以使用一个stack进行中序遍历,直到找到第K个元素;

2. 树树的结点数

对于每个节点root,计算以它为根节点的树的节点数,计作S。

如果S==K,返回root->val;

如果S > K,在root的左子树里面查找第K小元素;

如果S

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int kthSmallest(TreeNode root, int k) {
        Stack<TreeNode> s = new Stack<>();
        TreeNode p=root;
        s.push(p);
        int cnt=0;
        do{
            while(p!=null){
                s.push(p);
                p=p.left;
            }
            TreeNode top=s.pop();
            cnt++;
            if(cnt==k){
                return top.val;
            }
            p=top.right;
        }while(!s.empty());
        return 0;
    }
}

思路2代码:

/**
 * 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 kthSmallest(TreeNode* root, int k) {
        int left=findNodesSum(root->left);
        if(left+1==k){
            return root->val;
        }else if(left+1<k){
            return kthSmallest(root->right,k-left-1);
        }else{
            return kthSmallest(root->left,k);
        }
    }
    int findNodesSum(TreeNode* root){
        if(!root){
            return 0;
        }
        int sum = findNodesSum(root->left)+findNodesSum(root->right)+1;
        return sum;
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 07:04:37

leetcode--Kth Smallest Element in a BST的相关文章

LeetCode Kth Smallest Element in a BST(数据结构)

题意: 寻找一棵BST中的第k小的数. 思路: 递归比较方便. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 publi

[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] 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 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从零单刷】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

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