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

(medium)LeetCode 230.Kth Smallest Element in a BST

时间:2015-08-05 19:57:54      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

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.

Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

代码1:

/**
 * 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) {
        int count =countNodes(root.left);
        if(k<=count){
            return kthSmallest(root.left,k);
        }else if(k>count+1){
            return  kthSmallest(root.right,k-1-count);
        }
        return root.val;
    }
    public int countNodes(TreeNode n){
        if(n==null) return 0;
        return 1+countNodes(n.left)+countNodes(n.right);
    }
    
}

  运行结果:

       技术分享

代码2:中序遍历递归

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private static int number=0;
    private static int count=0;
    public int kthSmallest(TreeNode root, int k) {
        count=k;
        helper(root);
        return number;
    }
    public void helper(TreeNode n){
        if(n.left!=null) helper(n.left);
        count--;
        if(count==0){
            number=n.val;
            return;
        }
        if(n.right!=null) helper(n.right);
    }
    
}

 运行结果:

技术分享 

代码3:中序遍历迭代

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

  运行结果:

       技术分享

代码4:使用队列,中序遍历,存储起来,然后出队n个元素即可。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    private Queue<TreeNode> queue=new LinkedList<TreeNode>();
    public int kthSmallest(TreeNode root, int k) {
       inOrder(root);
       TreeNode ret=null;
       while(k>0){
           ret=queue.poll();
           k--;
       }
       return ret.val;
    }
    public void inOrder(TreeNode root){
        if(root==null) return;
        if(root.left!=null) inOrder(root.left);
          queue.offer(root);
        if(root.right!=null) inOrder(root.right);
        
    } 
    
}

  运行结果:

技术分享

(medium)LeetCode 230.Kth Smallest Element in a BST

标签:

原文地址:http://www.cnblogs.com/mlz-2019/p/4705529.html

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