- 浏览: 183693 次
- 性别:
- 来自: 济南
文章分类
最新评论
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个元素是返回就可以了。代码如下:
我们还可以先计算出左子树的节点个数leftnum,左子树的元素都小于根节点的元素,我们用leftnum与k比较,如果k > leftnum, 则k不再左子树中,我们只需要在右子树中找到第k - leftnum个元素就可以了;如果k < leftnum, 我们继续在左子树中找第k个元素;如果相等,我们就返回左子树的根节点。代码如下:
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
给定一个二叉搜索树,从中找到第k小的元素。根据二叉搜索树的性质中序遍历正好是二叉搜索树的升序排列,当我们遍历到第k个元素是返回就可以了。代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { int count = 0; int result = 0; public int kthSmallest(TreeNode root, int k) { if(root == null) return 0; dfs(root, k); return result; } public void dfs(TreeNode root, int k) { if(root == null) return; dfs(root.left, k); if(++count == k) { result = root.val; return; } dfs(root.right, k); } }
我们还可以先计算出左子树的节点个数leftnum,左子树的元素都小于根节点的元素,我们用leftnum与k比较,如果k > leftnum, 则k不再左子树中,我们只需要在右子树中找到第k - leftnum个元素就可以了;如果k < leftnum, 我们继续在左子树中找第k个元素;如果相等,我们就返回左子树的根节点。代码如下:
/** * 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) { if(root == null) return 0; int left = getNode(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); } } public int getNode(TreeNode root) { if(root == null) return 0; return 1 + getNode(root.left) + getNode(root.right); } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 500Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 430Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 581Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 676Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 845Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 707For a undirected graph with tre ...
相关推荐
java java_leetcode题解之Kth Smallest Element in a BST.java
java java_leetcode题解之Kth Smallest Element in a Sorted Matrix.java
378.Kth_Smallest_Element_in_a_Sorted_Matrix有序矩阵中第K小的元素【LeetCode单
c c语言_leetcode题解之0230_kth_smallest_element_in_a_bst
703.Kth_Largest_Element_in_a_Stream数据流中的第K大元素【LeetCode单题讲解系列】
java java_leetcode题解之Kth Smallest Number in Multiplication Table.java
标题 "the kth smallest elem" 暗示我们要讨论的是如何在数组或集合中找到第k小的元素,这是一个常见的算法问题,在计算机科学和数据结构领域具有重要意义。描述中提到的 "Clion" 是一个流行的C/C++集成开发环境,...
Smallest Element in a BST Review:高效的一些建议 Tip:Elastic Search VS Solr Share:人的一生两个最大的财富是:你的才华和你的时间 Algorithm:LC 763. Partition Labels Review:Signs that You are a Bad ...
lru cache leetcode Coding-Interview A repo for popular coding interview ...In ...In ...In ...二叉树查找/BST查找 Closest Binary Search Tree Value 二叉树查找/二叉树第K个 Kth Smallest Element In A
5. 题目230 - "Kth Smallest Element in a BST" 在二叉搜索树中找到第k小的元素,可以使用中序遍历。Java中,可以定义一个迭代或递归的中序遍历函数,同时维护一个计数器,当计数器等于k时返回当前元素。 6. 题目6...
this function returns kth smallest element in arr[l...r],using quicksort based method. without sorting all the elements in the list
c++ C++_leetcode题解之668_Kth_Smallest_Number_in_Multiplication_Table
java入门 java_leetcode题解之215_Kth_Largest_Element_in_an_Array
python python_leetcode题解之215_Kth_Largest_Element_in_an_Array.py
8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 Insert Interval 45 13 Two Sum 47 14 Two Sum II Input array is sorted 49 ...
标题中的“kth.zip_KTH”可能是指一个与KTH(瑞典皇家理工学院)相关的项目或工具,但根据描述,这个程序实际上是一个用于AutoCAD或其他类似CAD软件的脚本或插件,名为“块替换kth.lsp”。这个工具允许用户在图形...
题目2:Kth Largest Element in an Array 搜索算法题目 题目1:二分查找 题目2:搜索旋转排序数组 动态规划题目 题目1:斐波那契数列 题目2:最长公共子序列 贪心算法题目 题目1:分发饼干 题目2:跳跃游戏 回溯算法...
smallest or largest element in array 4. Improvements in Java 8 - For HashMap if hashcode collision count is more than 8 in a row , then its internal structure is changed from linked list to tree 渐近...
《KTH-TIPS纹理材质数据集详解》 纹理和材质是计算机图形学中不可或缺的元素,它们赋予虚拟世界中的物体以真实感和视觉吸引力。KTH-TIPS纹理材质数据集,作为一个广泛使用的资源库,为研究者和开发者提供了丰富的...