首页 > 其他 > 详细

[LeetCode]Kth Smallest Element in a BST

时间:2015-11-27 17:30:33      阅读:172      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!