Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {

int counter = 0;
public int kthSmallest(TreeNode root, int k) {
return KSmallest(root, k);
}

int KSmallest(TreeNode root, int k) {

if(root == null)
return -1;


int left = KSmallest(root.left, k);

counter++;
if(counter == k)
return root.val;

int right = KSmallest(root.right, k);
if(left != -1)
return left;
return right;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.



Example 1:


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