InterviewStack.io LogoInterviewStack.io

Trees and Binary Search Trees Questions

Hierarchical structures: binary trees, binary search trees, balanced trees, and tries. Covers traversal orders (in/pre/post-order, level-order), insertion and deletion invariants, and using tree properties to achieve logarithmic search. A core mid-difficulty interview area and the basis for many indexing and lookup systems.

MediumTechnical
49 practiced

Implement isBalanced(root) to check whether a binary tree is height-balanced (for every node, left and right heights differ by at most 1) in O(n) time. Avoid repeated height computations by using a helper that returns height or a sentinel for imbalance. Provide proof sketch for correctness.

HardTechnical
56 practiced

Given a Binary Search Tree that may be highly unbalanced, implement a Java function to transform it into a height-balanced BST. Signature: TreeNode balanceBST(TreeNode root). Provide the approach of extracting inorder traversal to a sorted list and rebuilding a balanced BST, explain time/space complexity, and contrast this with incremental rotation-based rebalancing (AVL/Red-Black) in terms of complexity and applicability.

HardTechnical
62 practiced

Write isIsomorphic(t1, t2) that checks if two binary trees are structurally identical and node values match. Then extend to check for isomorphism under optional mirror (children of a node may be swapped). Provide a recursive algorithm and analyze time and space complexity, including handling of nulls and leaf nodes.

HardSystem Design
56 practiced

Design a thread-safe concurrent BST for a multi-core environment. Describe possible locking strategies (coarse-grained global lock, fine-grained node locks or lock coupling, optimistic concurrency, and lock-free options), reasoning about deadlocks, contention, and performance. Outline insert/delete/search for your chosen approach.

HardTechnical
84 practiced

Design memory-efficient serialize(root) and deserialize(data) functions in Python for a binary tree using preorder traversal with null markers. The tree can be extremely large (up to 10 million nodes) so describe streaming serialization (emit tokens to disk or network), chunking approach, and how to implement deserialization with minimal peak memory. Discuss trade-offs between compactness, streaming ability, and error recovery.

Unlock Full Question Bank

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

Sign in to Continue

Join thousands of developers preparing for their dream job.