Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
O(k)
time and O(1)
space complexity without the recursive stack.HAPPY CASE
Input: root = [3,1,4,null,2], k = 1
Output: 1
Input: root = [5,3,6,2,4,null,null,1], k = 3
Output: 3
EDGE CASE
Input: root = [111], k = 1
Output: 111
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
If you are dealing with Binary Trees some common techniques you can employ to help you solve the problem:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Recursive in-order traversal to help identify the kth node.
1) Create a helper function that processes in-order traversal while reducing k to help identify kth node.
a) Base Case: Check if the tree is empty or k is less than 0. If it is, return. We have completed search.
b) Call helper on left child
c) Reduce k and check if k is 0, if so set kth node and return
d) Call helper on right child
2) Create variable to hold kthNode
3) Call helper function on root
4) Return kthNode
⚠️ Common Mistakes
Implement the code to solve the algorithm.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def kthSmallest(self, root: TreeNode, k: int) -> int:
# Create a helper function that processes in-order traversal while reducing k to help identify kth node
def helper(root: TreeNode):
nonlocal kthNode
nonlocal k
# Base Case: Check if the tree is empty or k is less than 0. If it is, return. We have completed search.
if not root or k < 0:
return
# Call helper on left child
helper(root.left)
# Reduce k and check if k is 0, if so set kth node and return
k -= 1
if k == 0:
kthNode = root.val
return
# Call helper on right child
helper(root.right)
# Create variable to hold kthNode
kthNode = 0
# Call helper function on root
helper(root)
# Return kthNode
return kthNode
class Solution {
// Create variable to hold kthNode
int ans = 0, i = 0;
public int kthSmallest(TreeNode root, int k) {
// Call helper function on root
inorder(root, k);
// Return kthNode
return ans;
}
// Create a helper function that processes in-order traversal while reducing k to help identify kth node
public void inorder(TreeNode root, int k) {
// Base Case: Check if the tree is empty or k is less than 0. If it is, return. We have completed search.
if(root == null || k < 0) {
return;
}
// Call helper on left child
inorder(root.left, k);
// Reduce k and check if k is 0, if so set kth node and return
i++;
if(k==i){
ans = root.val;
return;
}
// Call helper on right child
inorder(root.right, k);
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of nodes in binary search tree and K
represents the kth node we want to find.