题目地址
https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/
题目描述
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.
Example 1:
Input: root = [3,1,4,null,2], k = 1
3
/ \
1 4
\
2
Output: 1
Example 2:
Input: root = [5,3,6,2,4,null,null,1], k = 3
5
/ \
3 6
/ \
2 4
/
1
Output: 3
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?
思路
解法一:
由于‘中序遍历一个二叉查找树(BST)的结果是一个有序数组’ ,因此我们只需要在遍历到第k个,返回当前元素即可。
中序遍历相关思路请查看binary-tree-traversal
解法二:
联想到二叉搜索树的性质,root 大于左子树,小于右子树,如果左子树的节点数目等于 K-1,那么 root 就是结果,否则如果左子树节点数目小于 K-1,那么结果必然在右子树,否则就在左子树。
因此在搜索的时候同时返回节点数目,跟 K 做对比,就能得出结果了。
关键点解析
- 中序遍历
代码
解法一:
JavaScript Code:
/* * @lc app=leetcode id=230 lang=javascript * * [230] Kth Smallest Element in a BST */ /** * 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) { const stack = [root]; let cur = root; let i = 0; function insertAllLefts(cur) { while(cur && cur.left) { const l = cur.left; stack.push(l); cur = l; } } insertAllLefts(cur); while(cur = stack.pop()) { i++; if (i === k) return cur.val; const r = cur.right; if (r) { stack.push(r); insertAllLefts(r); } } return -1; };
Java Code:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ private int count = 1; private int res; public int KthSmallest (TreeNode root, int k) { inorder(root, k); return res; } public void inorder (TreeNode root, int k) { if (root == null) return; inorder(root.left, k); if (count++ == k) { res = root.val; return; } inorder(root.right, k); }
解法二:
JavaScript Code:
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ function nodeCount(node) { if (node === null) return 0; const l = nodeCount(node.left); const r = nodeCount(node.right); return 1 + l + r; } /** * @param {TreeNode} root * @param {number} k * @return {number} */ var kthSmallest = function(root, k) { const c = nodeCount(root.left); if (c === k - 1) return root.val; else if (c < k - 1) return kthSmallest(root.right, k - c - 1); return kthSmallest(root.left, k) };
扩展
这道题有一个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?
大家可以思考一下。
LeetCode题解215.kth-largest-element-in-an-array题目地址 https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目描述 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted or…
bulit-in gradle插件的版本号是多少? - java在我的gradle构建文件中,我有以下插件块plugins { `java-library` jacoco checkstyle } 这些都没有指定版本,但是一切正常。假定一个项目正在使用gradle 6.0和gradle包装器,但是系统已安装gradle 5.0。问题:如果我运行gradle wrapper ./gradlew build,将会执行grad…
LeetCode题解169.majority-element题目地址 https://leetcode.com/problems/majority-element/description/ 题目描述 Given an array of size n, find the majority element. The majority element is the element that appears more tha…
LeetCode题解1015.smallest-integer-divisible-by-k题目地址 https://leetcode-cn.com/problems/smallest-integer-divisible-by-k/description/ 题目描述 给定正整数 K,你需要找出可以被 K 整除的、仅包含数字 1 的最小正整数 N。 返回 N 的长度。如果不存在这样的 N,就返回 -1。 示例 1: 输入:1 输出:1 解释:最小…
LeetCode题解1104.path-in-zigzag-labelled-binary-tree题目地址 https://leetcode-cn.com/problems/path-in-zigzag-labelled-binary-tree/description/ 题目描述 在一棵无限的二叉树上,每个节点都有两个子节点,树中的节点 逐行 依次按 “之” 字形进行标记。 如下图所示,在奇数行(即,第一行、第三行、第五行……)中,按从左到右的顺序进行标…