Repository navigation
Add unit tests for the "Kth Smallest Element in a BST" problem. #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
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
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/main/java/com/leetcode/medium/KthSmallestElementInABST230.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Tags: Tree, Binary Search Tree, DFS | ||
| package com.leetcode.medium; | ||
|
|
||
| import com.leetcode.datastructure.TreeNode; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class KthSmallestElementInABST230 { | ||
| private void inorder(TreeNode node, List<Integer> res) { | ||
| if (node == null) { | ||
| return; | ||
| } | ||
| inorder(node.left, res); | ||
| res.add(node.val); | ||
| inorder(node.right, res); | ||
| } | ||
|
|
||
| /* | ||
| Time complexity : O(N) to build a traversal. | ||
| Space complexity : O(N) to keep an inorder traversal. | ||
| */ | ||
| public int kthSmallest(TreeNode root, int k) { | ||
| List<Integer> values = new ArrayList<>(); | ||
| inorder(root, values); | ||
| return values.get(k - 1); | ||
| } | ||
| } | ||
48 changes: 48 additions & 0 deletions
48
src/test/java/com/leetcode/medium/KthSmallestElementInABST230Test.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package com.leetcode.medium; | ||
|
|
||
| import com.leetcode.datastructure.TreeNode; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| public class KthSmallestElementInABST230Test { | ||
| private KthSmallestElementInABST230 solution; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| solution = new KthSmallestElementInABST230(); | ||
| } | ||
|
|
||
| @Test | ||
| void testKthSmallest_Example1() { | ||
| TreeNode root = new TreeNode(3, | ||
| new TreeNode(1, null, new TreeNode(2)), | ||
| new TreeNode(4)); | ||
|
|
||
| int k = 1; | ||
|
|
||
| int expected = 1; | ||
| int result = solution.kthSmallest(root, k); | ||
|
|
||
| assertEquals(expected, result); | ||
| } | ||
|
|
||
| @Test | ||
| void testKthSmallest_Example2() { | ||
| TreeNode root = new TreeNode(5, | ||
| new TreeNode(3, | ||
| new TreeNode(2, | ||
| new TreeNode(1), | ||
| null), | ||
| new TreeNode(4)), | ||
| new TreeNode(6)); | ||
|
|
||
| int k = 3; | ||
|
|
||
| int expected = 3; | ||
| int result = solution.kthSmallest(root, k); | ||
|
|
||
| assertEquals(expected, result); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method lacks validation for the parameter k. If k is less than 1 or greater than the number of nodes in the tree, this will throw an IndexOutOfBoundsException. Consider adding validation to check that k is within the valid range (1 <= k <= tree size) and throw a more meaningful exception (e.g., IllegalArgumentException) with a descriptive error message.