int kthSmallest(TreeNode* root, int k) {
     int res;
     // inorder traversal: recursive
     //inorder(root, k, res);
     // iterative
     
     /*stack<TreeNode*> s;
     int rank = 1;
     while(!s.empty() || root) {
         while(root) {
             s.push(root);
             root = root->left;
         }
         
         root = s.top()->right;
         if (rank == k) return s.top()->val;
         s.pop();
         rank++;
     }
     
     return INT_MAX; */
     //return res;
     
     
     // binary search
     int n = size(root->left);
     if (n + 1 == k) return root->val;
     else if (n + 1 < k) return kthSmallest(root->right, k - n - 1);
     else return kthSmallest(root->left, k);
     
 }
Exemple #2
0
	int kthSmallest(TreeNode* root, int k) {
		// if (root == NULL) return 0;
		int leftSize = calcTreeSize(root->left);
		if (k == leftSize+1) return root->val;
		else if (leftSize >= k) return kthSmallest(root->left,k);
		else return kthSmallest(root->right, k-leftSize-1);
	}
 int kthSmallest(TreeNode* root, int k) {
     int c = cnt(root->left);
     if(k <= c)
         return kthSmallest(root->left, k);
     else if(k > c+1)
         return kthSmallest(root->right, k-c-1);
     return root->val;
 }
Exemple #4
0
 // 递归
 int kthSmallest(TreeNode* root, int &k) {
     if (!root) return 0;
     int left = kthSmallest(root->left, k);
     if (left) return left;
     --k;
     if (k == 0) return root->val;
     int right = kthSmallest(root->right, k);
     return right;
 }
Exemple #5
0
 int kthSmallest(TreeNode* root, int k) {
     int cnt = count(root->left);
     if(k == cnt + 1)
         return root->val;
     else if(k <= cnt)
         return kthSmallest(root->left, k);
     else
         return kthSmallest(root->right, k-cnt-1);
 }
Exemple #6
0
int kthSmallest(TreeNode* root, int k) {
	int num = countNode(root);
	int n = countNode(root->right);
	if (k == num - n)
		return (root -> val);
	if (k < num - n)
		return kthSmallest(root -> left, k);
	else return kthSmallest(root -> right, k - num + n);
}
 bool kthSmallest(TreeNode* root, int k, int& r) {
     if (!root) { r=0; return false;}
     if (kthSmallest(root->left, k, r)) return true;
     if (k == r + 1) {r=root->val; return true;}
     int count = r + 1;
     if (kthSmallest(root->right, k-count, r)) return true;
     r += count;
     return false;
 }
 int kthSmallest(TreeNode* root, int k) {
     int count = countLeftNode(root->left);
     if(k < count + 1)
     	return kthSmallest(root->left, k);
     else if(k > count + 1)
     	return kthSmallest(root->right, k - 1 - count);
     else
     	return root->val;
 }
 void kthSmallest(TreeNode* root, int& k, int& ans) {
     if (!root) return;
     kthSmallest(root->left, k, ans);
     if (--k == 0) {
         ans = root->val;
         return;
     }
     kthSmallest(root->right, k, ans);
 }
Exemple #10
0
int kthSmallest(struct TreeNode* root, int k) {
    int lcount = calTreeNodeCount(root->left);
    if (lcount < k-1){
        return kthSmallest(root->right, k-lcount-1);
    }else if (lcount > k-1){
        return kthSmallest(root->left, k);
    }else{
        return root->val;
    }
}
    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;
    }
 int kthSmallest(TreeNode* root, int k) {
     int left_depth = depth(root->left);
     if (left_depth + 1 == k) {
         return root->val;
     } else if (left_depth + 1 < k) {
         return kthSmallest(root->right, k - left_depth -1);
     } else {
         return kthSmallest(root->left, k);
     }
 }
 int kthSmallest(TreeNode* root, int k) {
     int n = countNodes(root->left);
     if (n >= k) {
         return kthSmallest(root->left, k);
     } else if (n < k - 1) {
         return kthSmallest(root->right, k - n - 1);
     } else { // n == k - 1
         return root->val;
     }
 }
int kthSmallest(struct TreeNode *root, int k)
{
	int c = count(root->left);
	c++;
	if (c == k)
		return(root->val);
	if (c > k)
		return(kthSmallest(root->left, k));
	return(kthSmallest(root->right, k - c));
}
 int kthSmallest(TreeNodeWithCount* rootWithCount, int k) {
     if(k <= 0 || k > rootWithCount->count) return -1;
     if(rootWithCount->left) {
         if(rootWithCount->left->count >= k) return kthSmallest(rootWithCount->left, k);
         if(rootWithCount->left->count == k - 1) return rootWithCount->val;
         return kthSmallest(rootWithCount->right, k - 1 - rootWithCount->left->count);
     }else {
         if(k == 1) return rootWithCount->val;
         else return kthSmallest(rootWithCount->right, k - 1);
     }
 }
Exemple #16
0
int kthSmallest(struct TreeNode* root, int k) {
    if (root == NULL || k < 0) return 0;
    int leftNodes = findNodesSum(root->left);
        
    if (leftNodes + 1 == k)
        return root->val;
    else if (leftNodes + 1 > k)
        return kthSmallest(root->left, k);
    else
        return kthSmallest(root->right, k - leftNodes - 1);
}
 int kthSmallest(struct TreeNode* root, int k) {
 	int n = 0;
    
    n = countNodes(root->left);
 	if ((k-1) > n)
 		return kthSmallest(root->right, k-n-1);
 	else if ((k-1) < n)
 		return kthSmallest(root->left, k);
 	else 
 		return root->val;
 }
 int kthSmallest(TreeNode *root, int k)
 {
     if (!root) return -1;
     int left = root->left ? root->left->cnt : 0;
     int right = root->right ? root->right->cnt : 0;
     if (k == root->cnt - right)
         return root->val;
     else if (k < root->cnt - right)
         return kthSmallest(root->left, k);
     else return kthSmallest(root->right, k - left - 1);
 }
  int kthSmallest(TreeNode *root, int k) {
    if (root == NULL) return 0;
    int lnum = countNodes(root->left);

    if (k == lnum + 1) {
      return root->val;
    } else if (k > lnum + 1) {
      return kthSmallest(root->right, k - lnum - 1);
    } else {
      return kthSmallest(root->left, k);
    }
  }
	int kthSmallest(TreeNode* root, int& k) 
	{
		if (nullptr == root)
			return INT_MAX;

		int res = kthSmallest(root->left, k);
		if (res != INT_MAX)
			return res;
		if (0 == --k)
			return root->val;
		res = kthSmallest(root->right, k);
		if (res != INT_MAX)
			return res;
	}
 int kthSmallest(TreeNode *root, int &count, int k) {
     if (NULL == root) {
         return 0;
     }
     int left = kthSmallest(root->left, count, k);
     if (count >= k) {
         return left;
     }
     ++ count;
     if (count >= k) {
         return root->val;
     }
     int right = kthSmallest(root->right, count, k);
     return right;
 }
    /**
     * @param matrix: a matrix of integers
     * @param k: an integer
     * @return: the kth smallest number in the matrix
     */
    int kthSmallest(vector<vector<int> > &matrix, int k) {
        // write your code here
		if (0 == matrix.size() || 0 == matrix[0].size()) {
			return 0;
		}
		else if (k <= 1) {
			return matrix[0][0];
		}
		else {
			matrix[0][0] = matrix[matrix.size() - 1][matrix[matrix.size() - 1].size() - 1];
			size_t i = 0;
			size_t j = 0;
			while (i < matrix.size() && j < matrix[matrix.size() - 1].size()) {
				if ((j + 1 >= matrix[matrix.size() - 1].size() || (i + 1 < matrix.size() && j + 1 < matrix[matrix.size() - 1].size() && matrix[i + 1][j] < matrix[i][j + 1])) && matrix[i][j] > matrix[i + 1][j]) {
					int temp = matrix[i][j];
					matrix[i][j] = matrix[i + 1][j];
					matrix[i + 1][j] = temp;
					++ i;
				}
				else if ((i + 1 >= matrix.size() || (i + 1 < matrix.size() && j + 1 < matrix[matrix.size() - 1].size() && matrix[i + 1][j] >= matrix[i][j + 1])) && matrix[i][j] > matrix[i][j + 1]) {
					int temp = matrix[i][j];
					matrix[i][j] = matrix[i][j + 1];
					matrix[i][j + 1] = temp;
					++ j;
				}
				else {
					break;
				}
			}
			return kthSmallest(matrix, k - 1);
		}
    }
    void kthSmallest(TreeNode* root, int k, int *count,int *ans)
    {
       if(root)
       {
           kthSmallest(root->left,k,count,ans);
           *count += 1;
           if(*count ==k)
           {
               *ans = root->val;
               return;
           }
           if(*ans == INT_MAX)
                kthSmallest(root->right,k,count,ans);
       }

    }
    int kthSmallest(vector<int>& arr, int l, int r, int k)
    {

        if (k > 0 && k <= r - l + 1)
        {
            int pos = randomPartition(arr, l, r);
        
            if (pos-l == k-1)
                return arr[pos];
            else if(pos-l > k-1)
                return kthSmallest(arr, l, pos-1, k);
            else
                return kthSmallest(arr, pos+1, r, k-pos+l-1);
        }
        return INT_MAX;
    }
Exemple #25
0
int _tmain(int argc, _TCHAR* argv[])
{
    int list1[] = { 3, 4, 10, 23, 45, 55, 56, 58, 60, 65 };
    int list2[] = { 3, 3, 3, 15, 16, 28, 50, 70, 71, 72 };
    int k = 11;
    int kSmallest = kthSmallest(list1, list2, k);
    return 0;
}
Exemple #26
0
int kthSmallest(int arr[], int l, int r, int k){
	if(k>0 && k <= r - l + 1){

		int pos = randomisedPartition(arr, l, r);
		if(pos - l == k - 1){
			return arr[pos];
		}
		else if(pos - l > k - 1){
			// the element is in the left sub array
			// recurse on the left sub array
			return kthSmallest(arr, l, pos - 1, k);
		}
		else{
			return kthSmallest(arr, pos+1, r, k - pos + l - 1);
		}
	}
	return INT_MAX;
}
int main() {
    struct TreeNode* root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    root->val = 2;
    struct TreeNode* left = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    left->val = 1;
    struct TreeNode* right = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    right->val = 3;
    root->left = left;
    root->right = right;
    left->left = NULL;
    left->right = NULL;
    right->left = NULL;
    right->right = NULL;
    assert(kthSmallest(root, 2) == 2);

    return 0;
}
   // method 1
   // go leftmost first
   // then k decrease
int kthSmallest(TreeNode* root, int& k) {
	if (root) {
		int x = kthSmallest(root->left, k);
		return !k ? x : (!--k ? root->val : kthSmallest(root->right, k));
	}
}
 int findKthLargest(vector<int>& nums, int k) {
     return kthSmallest(nums, 0, nums.size()-1, nums.size()-k+1);
 }
 int kthSmallest(TreeNode* root, int k) {
     TreeNodeWithCount* rootWithCount = buildTreeWithCount(root);
     return kthSmallest(rootWithCount, k);
 }