1. 程式人生 > >leetcode--Kth Smallest Element in a BST

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;
    }
};

相關推薦

[LeetCode] 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

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 alway

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

-o bsp ive http always += cnblogs trac check Given a binary search tree, write a function kthSmallest to find the kth smallest element in

Leetcode 230: Kth Smallest Element in a BST

pre pop valid operation order not may nodes tree Given a binary search tree, write a function kthSmallest to find the kth smallest elemen

LeetCode-230 kth smallest element in a bst 二叉搜尋樹中第K小的元素

題目連結 https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/ 題意 中文題,對於二叉搜尋樹而言,找其中的第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 alwa

#Leetcode# 230. Kth Smallest Element in a BST

https://leetcode.com/problems/kth-smallest-element-in-a-bst/   Given a binary search tree, write a function kthSmallest to find the k

LeetCode】 230. Kth Smallest Element in a BST(非遞迴中序遍歷二叉樹)

因為這是一棵二叉搜尋樹,所以找它的第 kkk 小值,就是這棵二叉搜尋樹的中序遍歷之後的第 kkk 個數,所以只需要將這棵樹進行中序遍歷即可,下面程式碼是非遞迴形式的二叉樹中序遍歷。 程式碼如下: /**

LeetCode刷題Medium篇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

leetcode 230. Kth Smallest Element in a BST

pan class http malle 就會 blog 返回值 替代 如果 https://www.cnblogs.com/grandyang/p/4620012.html 這個題其實就是中序遍歷第k個數就好了,代碼最好寫的就是非遞歸的方式,在stack裏面找第k個就好

230. Kth Smallest Element in a BST

pro new lee public http ack urn nod light https://leetcode.com/problems/kth-smallest-element-in-a-bst/#/solutions public int kthSmallest

Google面試題專題6 - leetcode230. Kth Smallest Element in a BST/139. Word Break

230. Kth Smallest Element in a BST 題目描述 給定一顆二叉搜尋樹,編寫函式kthSmallest找到第k小元素。(1 ≤ k ≤ BST的總元素個數) 例子 Example 1: Input: root = [3,1,4,nul

Kth Smallest Element in a BST 二叉搜尋樹中第K小的元素

給定一個二叉搜尋樹,編寫一個函式kthSmallest來查詢其中第 k 個最小的元素。說明:你可以假設 k 總是有效的,1 ≤ k ≤ 二叉搜尋樹元素個數。示例 1:輸入: root = [3,1,4,null,2], k = 1 輸出: 1示例 2:輸入: root = [

[LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩陣中第K小的元素

Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is the

230 Kth Smallest Element in a BST

題目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in

[Swift]LeetCode230. 二叉搜尋樹中第K小的元素 | 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 vali

230. Kth Smallest Element in a BST - Medium

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always vali

找第k大的專題: 378. Kth Smallest Element in a Sorted Matrix && 230. Kth Smallest Element in a BST

找第k大的專題 378. Kth Smallest Element in a Sorted Matrix 1. 題目描述 題目連結 Given a n x n matrix where each of the rows and columns are s

Leetcode】378. Kth Smallest Element in a Sorted Matrix

之前 prior return 例如 mat all 查找 row 所在 Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, f

[leetcode]378. Kth Smallest Element in a Sorted Matrix

[leetcode]378. Kth Smallest Element in a Sorted Matrix Analysis 今天要吃柚子—— [每天刷題並不難0.0] Given a n x n matrix where each of the rows