【树】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.

思路:

1、计算左子树元素个数leftSize。

2、 leftSize+1 = K,则根节点即为第K个元素

leftSize >=k, 则第K个元素在左子树中,

leftSize +1 <k, 则转换为在右子树中,寻找第K-left-1元素。

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} k
 * @return {number}
 */
var kthSmallest = function(root, k) {
    if(root==null){
        return 0;
    }
    var leftSize=nodeNum(root.left);

    if(k==leftSize+1){
        return root.val;
    }else if(k<leftSize+1){
        return kthSmallest(root.left,k);
    }else{
        return kthSmallest(root.right,k-leftSize-1);
    }
};

function nodeNum(root){
    if(root==null){
        return 0;
    }else{
        return 1+nodeNum(root.left)+nodeNum(root.right);
    }
}
时间: 2024-07-31 14:47:45

【树】Kth Smallest Element in a BST(递归)的相关文章

【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

[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

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

(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

[leedcode 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(46):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小的数,不就是遍历到第k个数吗?所以中序遍历很容易想到,如下: /** * Definition for a binary tree node.