230. Kth Smallest Element in a BST

230. Kth Smallest Element in a BST .

 


Find the kth smallest element in the tree: 


Log n to reach the smallest element 


Log n + k (amortized)

Given a binary search tree, find the fifth smallest node.not guarantee there are more than five nodes in the tree) if you are using recursion, no global variable please.
230. Kth Smallest Element in a BST 
=============================
// naive solution: get k nodes sorted in the bst and get the kth node 
//record the first k nodes , return the kth node 


class Solution {
  
    // global var 
    List<Integer> result = new ArrayList<>();
    public int kthSmallest(TreeNode root, int k) {
      if(root == null) return 0;
      
      inOrder(root, k);
      
      return result.get(k - 1);  
    }
    private void inOrder(TreeNode root, int k){
      if(result.size() > k) return;
      
      // base case
      if(root == null) return;
      
      // recursion
      inOrder(root.left, k);
      result.add(root.val);
      inOrder(root.right, k);
    }
}


=====================================
dfs inroder traversal recursively 

// difference from solution 1: 

didn’t record the first k nodes 
class Solution {
    // why private gobal var ?
    private int result = 0;
    private int count;
    
    public int kthSmallest(TreeNode root, int k) {
      count = k; //////
      inOrder(root);
      return result;
    
    }
    private void inOrder(TreeNode root){   //// void 
      if(root.left != null) inOrder(root.left);
      
      count--;
      if(count == 0){
        result = root.val;
        return;
      }
      
      if(root.right != null) inOrder(root.right);
    }
  
}



=====================================
in-order iterative:


/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int kthSmallest(TreeNode root, int k) {
      Stack<TreeNode> stack = new Stack<>();
      pushLeft(root, stack);
      
      while( k > 0){
        TreeNode cur = stack.pop();
        k--;
        if(k == 0) return cur.val;
        if(cur.right != null){
          pushLeft(cur.right, stack);
        }
      }
      return -1;
    }
    private void pushLeft(TreeNode node, Stack<TreeNode> stack){
      while(node != null){
        stack.push(node);
        node = node.left;
      }
    }
}

=====================================
kth largest 
(reverse in order , from right to left)



class Solution {
    public int kthLargest(TreeNode root, int k) {
      Stack<TreeNode> stack = new Stack<>();
      pushRight(root, stack);
      
      while( k > 0){
        TreeNode cur = stack.pop();
        k--;
        if(k == 0) return cur.val;
        if(cur.left != null){
          pushRight(cur.left, stack);
        }
      }
      return -1;
    }
    private void pushRight(Stack<TreeNode> stack, int node){
      while(node != null){
        stack.push(node);
        node = node.right;
      }
    }
}

 




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.

Example 1:

Input: root = [3,1,4,null,2], k = 1
   3
  / \
 1   4
  \
   2
Output: 1

Example 2:

Input: root = [5,3,6,2,4,null,null,1], k = 3
       5
      / \
     3   6
    / \
   2   4
  /
 1
Output: 3

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?

 

posted on 2018-08-09 18:23  猪猪&#128055;  阅读(108)  评论(0)    收藏  举报

导航