【LeetCode】Kth Smallest Element in a BST

原创 2015年07月06日 17:15:12

问题描述

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.

算法思想

BST中序遍历为排序序列,直接技术前k个访问的数即可得到第k个最小的元素

算法实现

public class Solution {
    private int num = 0;
    private int c = 0;
    public void helper(TreeNode root, int k) {
        if(root.left != null) {
            helper(root.left, k);
        }

        c++;
        if (k == c){
            num = root.val;
            return;
        }

        if (root.right != null) {
            helper(root.right, k);
        }
    }
    public int kthSmallest(TreeNode root, int k) {
         helper(root,k);
         return num;
    }
}

算法时间

T(n) = O(lgn)

演示结果

这里写图片描述

public static void main(String[] args) {
        TreeNode root = new TreeNode(8);
        TreeNode l1 = new TreeNode(1);
        TreeNode l2 = new TreeNode(3);
        TreeNode l3 = new TreeNode(6);
        TreeNode l4 = new TreeNode(4);
        TreeNode l5 = new TreeNode(7);
        TreeNode l6 = new TreeNode(10);
        TreeNode l7 = new TreeNode(14);
        TreeNode l8 = new TreeNode(13);
        root.left = l2;
        root.right = l6;
        l2.left = l1;
        l2.right = l3;
        l3.left = l4;
        l3.right = l5;
        l6.right = l7;
        l7.left = l8;
        Solution s = new Solution();
        s.kthSmallest(root,3);
        System.out.println(s.num);
    }

4

版权声明:本文为博主原创文章,欢迎转载并接受错误指正。

相关文章推荐

leetcode-230-Kth Smallest Element in a BST

Kth Smallest Element in a BST   Given a binary search tree, write a function kthSmallest to ...

[leetcode] Kth Smallest Element in a BST

题目链接在此 Given a binary search tree, write a function kthSmallest to find the kth smallest elemen...

leetcode 230. Kth Smallest Element in a BST-递归|非递归

原题链接:230. Kth Smallest Element in a BST 【思路-Java、Python】——递归实现 我们知道二分查找数(BST)的性质——任何一个节点的值均大于左子树的任意节...

<LeetCode OJ> 230. Kth Smallest Element in a BST

230. Kth Smallest Element in a BST My Submissions Question Total Accepted: 32753 Total Submissio...

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

LeetCode - 230. Kth Smallest Element in a BST

这道题目需要利用BST的性质,就是如果对BST进行中序遍历的话,得到的序列的一定是sorted,所以一开始使用了一个ArrayList存储访问的元素,然后返回第k - 1个。但是这种方法不太好,更好的...

Leetcode 230. Kth Smallest Element in a BST (Medium) (cpp)

Leetcode 230. Kth Smallest Element in a BST (Medium) (cpp)

LeetCode 230. Kth Smallest Element in a BST 解题报告

LeetCode 230. Kth Smallest Element in a BST 解题报告

Kth Smallest Element in a BST - leetcode 230号题目个人题解

Kth Smallest Element in a BST - leetcode 230号题目个人题解题目要求Given a binary search tree, write a function ...

[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.No...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)