`
hcx2013
  • 浏览: 90711 次
社区版块
存档分类
最新评论

Kth Smallest Element in a BST

 
阅读更多

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

 

/**
 * 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) {
        	Stack<TreeNode> stack = new Stack<>();
        	while (!stack.isEmpty() || root != null) {
        		if (root != null) {
        			stack.push(root);
        			root = root.left;
        		} else {
        			root = stack.pop();
        			if (--k == 0) {
        				return root.val;
        			}
        			root = root.right;
        		}
        	}
        }
        return 0;
    }
}

 

分享到:
评论

相关推荐

    java-leetcode题解之Kth Smallest Element in a BST.java

    在Java语言实现的LeetCode题解中,"Kth Smallest Element in a BST"这一问题尤为引人注目,它涉及到了二叉树的遍历以及对树的特性进行深度利用。 二叉搜索树具有独特的特性:对于任意节点,其左子树中的所有元素都...

    java-leetcode题解之Kth Smallest Element in a Sorted Matrix.java

    在处理这个问题之前,我们首先需要理解题目“Kth Smallest Element in a Sorted Matrix”的意思。这道题是LeetCode上的算法题目,要求求解在一个按行和列都排好序的矩阵中,找到第k小的元素。此类问题通常涉及到矩阵...

    378.Kth Smallest Element in a Sorted Matrix有序矩阵中第K小的元素【LeetCode单题讲解系列】

    378.Kth_Smallest_Element_in_a_Sorted_Matrix有序矩阵中第K小的元素【LeetCode单

    c语言-leetcode题解之0230-kth-smallest-element-in-a-bst

    c c语言_leetcode题解之0230_kth_smallest_element_in_a_bst

    703.Kth Largest Element in a Stream数据流中的第K大元素【LeetCode单题讲解系列】

    703.Kth_Largest_Element_in_a_Stream数据流中的第K大元素【LeetCode单题讲解系列】

    the kth smallest elem

    标题 "the kth smallest elem" 暗示我们要讨论的是如何在数组或集合中找到第k小的元素,这是一个常见的算法问题,在计算机科学和数据结构领域具有重要意义。描述中提到的 "Clion" 是一个流行的C/C++集成开发环境,...

    leetcode1124java-arts:艺术

    Smallest Element in a BST Review:高效的一些建议 Tip:Elastic Search VS Solr Share:人的一生两个最大的财富是:你的才华和你的时间 Algorithm:LC 763. Partition Labels Review:Signs that You are a Bad ...

    lrucacheleetcode-Coding-Interview:编程面试

    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

    java-leetcode题解之Kth Smallest Number in Multiplication Table.java

    在处理多层嵌套循环生成的乘法表时,寻找第k小的数是一项具有挑战性的任务,需要高效的算法来优化查找性能。乘法表通常是一个矩阵,其行和列代表两个自然数序列的乘积,例如,第一行是1乘以1到n,第二行是2乘以1到n...

    Selective_Leetcode_solutions

    5. 题目230 - "Kth Smallest Element in a BST" 在二叉搜索树中找到第k小的元素,可以使用中序遍历。Java中,可以定义一个迭代或递归的中序遍历函数,同时维护一个计数器,当计数器等于k时返回当前元素。 6. 题目6...

    QuickSelect算法

    this function returns kth smallest element in arr[l...r],using quicksort based method. without sorting all the elements in the list

    C++-leetcode题解之668-Kth-Smallest-Number-in-Multiplication-Table

    在解决LeetCode上的668题时,涉及到对乘法表中第k小的数进行寻找。乘法表是由连续的正整数所构成,其中第i行和第j列的数是i*j。题目要求我们找出这样的一个数,它是这个乘法表中按升序排列时的第k个数。...

    java-leetcode题解之215-Kth-Largest-Element-in-an-Array

    在这个上下文中,Java leetcode题解之215_Kth_Largest_Element_in_an_Array是一道旨在寻找数组中第k大元素的编程题。 要解决这个问题,我们需要理解算法的核心概念:如何高效地找到一个数组中的第k大元素。一个常见...

    Coding Interview In Java

    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 ...

    常见算法介绍、算法刷题(含解析与代码).rar

    题目2:Kth Largest Element in an Array 搜索算法题目 题目1:二分查找 题目2:搜索旋转排序数组 动态规划题目 题目1:斐波那契数列 题目2:最长公共子序列 贪心算法题目 题目1:分发饼干 题目2:跳跃游戏 回溯算法...

    kth.zip_KTH

    本文将详细探讨一个特别的工具——“块替换kth.lsp”,该工具能够实现CAD图形中块的批量替换功能,极大地简化了设计图纸的管理过程。 首先,让我们了解一下CAD软件中的“块”。在AutoCAD等软件中,“块”是一种特殊...

    纹理分类数据集kth-tips2-a_b.zip

    用于纹理图像分类的基准数据集,网上比较难找。...解压后包含kth-tips2-a_col_200x200.tar和kth-tips2-b_col_200x200.tar两个文件,a是每一类的样本数不一样,b是每一类的样本数一样,一般使用b作为数据集。

    python-leetcode题解之215-Kth-Largest-Element-in-an-Array.py

    在数据分析与算法实现中,常常需要寻找一组数据中的第k大的元素。在Python编程语言中,LeetCode网站提供了一个经典问题“215.数组中的第k个最大元素”,该问题要求使用算法找出一个未排序整数数组中第k大的元素。...

    leetcode分类-DataStructures:室内建筑的简单介绍和解释

    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 渐近...

Global site tag (gtag.js) - Google Analytics