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.

HardTechnical
59 practiced

You are reviewing a function that decides whether a binary tree is height-balanced. The current code recomputes subtree heights over and over, and it becomes slow on a skewed input. How would you rewrite it so each subtree is processed once, and how would you stop the recursion early when imbalance is detected?

MediumTechnical
83 practiced

Implement is_balanced(root) in Python to determine whether a binary tree is height-balanced (for every node, the heights of left and right subtrees differ by at most 1). Ensure an O(n) time implementation by computing heights bottom-up and short-circuiting on imbalance.

MediumTechnical
46 practiced

Implement an algorithm in Python to compute the diameter of a binary tree (number of nodes on the longest path between any two nodes). Your solution should run in O(n) time by computing height and diameter in a single pass. Explain edge cases such as empty trees and single-node trees and include a short example.

MediumTechnical
73 practiced

Design and implement two algorithms to validate whether a binary tree is a valid BST: (1) recursive method using allowable min/max ranges passed down the recursion, and (2) iterative method using stack and inorder traversal checking monotonicity. Both should be O(n) time. Explain how your approach handles duplicate keys under different duplicate policies.

EasyTechnical
57 practiced

Given the binary tree defined as: root=1, left child=2 (left=4, right=5), and right child=3 (left=null, right=6). Provide the output sequences for: (a) preorder, (b) inorder, (c) postorder, and (d) level-order traversals. Explain the steps that produce each sequence and comment on how iterative and recursive approaches produce the same ordering.

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.