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
Convert a BST into a sorted doubly linked list in-place where left pointers become previous and right pointers become next pointers. Implement in Python using O(h) extra space (recursion or stack), reconnect nodes in-order without creating new nodes, and return the head of the list. Provide an example and explain iterative in-order reconnection approach.
HardTechnical
0 practiced
Given two nodes in a binary tree where each node has a parent pointer but you are not given the root, design an O(h) time and O(1) extra space algorithm to find their Lowest Common Ancestor (LCA). Provide a Python-style pseudocode sketch and handle edge cases where nodes belong to different trees.
HardTechnical
0 practiced
Two nodes of a BST were swapped by mistake. Design and implement an algorithm in Python to recover the BST by swapping them back without changing the tree structure. Aim for O(n) time and O(h) space (or O(1) if using Morris traversal). Explain how to detect swapped nodes by scanning the inorder sequence and give code sketch for fixing them.
HardTechnical
0 practiced
Explain and implement Morris inorder traversal that visits nodes using O(1) extra space by temporarily modifying right pointers of predecessors. Provide Python implementation and discuss when it is safe to use Morris (for example, not safe with concurrent readers or immutable/persistent tree representations). Explain how to adapt Morris traversal to find the kth smallest in a BST without recursion or stack.
HardTechnical
0 practiced
Design and implement delete(key) for a BST where duplicate keys are allowed and represented by a 'count' integer stored at each node. Deletion should decrement count if greater than 1 and remove the node only when count reaches zero. Provide Python pseudocode, explain how search and traversal must adapt to counts, and discuss maintaining subtree sizes for order-statistics.

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.