码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Kth Smallest Element in a BST

时间:2015-11-27 17:32:33      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

public class Solution {
    TreeNode result = new TreeNode(0);
    public int kthSmallest(TreeNode root, int k) {
        helper(root, k);
        return result.val;
    }
    public int helper(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        int left = helper(root.left, k);
        if (left == k - 1) {
            result = root;
        }
        int right = helper(root.right, k - left - 1);
        return left + right + 1;
    }
}

 

[LeetCode]Kth Smallest Element in a BST

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5001088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!