InterviewStack.io LogoInterviewStack.io

Binary Trees and Binary Search Trees Questions

Focuses on tree data structures, specifically binary trees and binary search trees. Candidates should understand node relationships, common traversals including in order, pre order, post order, and level order, and be able to implement traversals both recursively and iteratively. Cover binary search tree properties and operations including search, insertion, deletion, validation of binary search tree property, and finding the lowest common ancestor. Include problems on tree paths, height and balance calculations, serialization and deserialization, checking and restoring balance at a high level, and use cases in system design. Emphasize complexity analysis, recursion versus iterative solutions using stacks or queues, and handling edge cases such as duplicate keys and degenerate trees.

MediumTechnical
0 practiced
Implement a function to find the lowest common ancestor (LCA) of two nodes in: a) a binary search tree using BST properties in O(h) time, and b) a general binary tree without BST properties using recursion in O(n) time. Provide code in Python and explain correctness and edge cases where one or both nodes are absent.
HardTechnical
0 practiced
Design an algorithm to find the kth smallest element in a very large BST stored on disk where you only have limited memory (e.g., few MBs). Explain an approach that minimizes random disk seeks by streaming nodes sequentially and discuss worst-case IO and memory trade-offs. Optionally sketch code that uses iterators and external merging.
EasyTechnical
0 practiced
What is an in-order successor of a node in a BST? Provide the definition and describe two algorithms to find the in-order successor of a given node: one that assumes parent pointers exist and one that only has access to the tree root and target value. Discuss complexity.
MediumTechnical
0 practiced
Write a Python function to validate whether a binary tree is a valid binary search tree (BST). Your solution should be O(n) time and use O(h) space; accept a parameter that controls whether duplicates are allowed and, if allowed, whether they go to left or right. Explain correctness and edge cases such as large negative/positive values.
EasyTechnical
0 practiced
Write an iterative search function in Python to check whether a value exists in a binary search tree (BST). Your function should take the root and the target value and return True/False. Aim for O(h) average time and O(1) auxiliary space. Discuss how the runtime degrades for degenerate trees.

Unlock Full Question Bank

Get access to hundreds of Binary Trees and Binary Search Trees interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.