Trees and Binary Search Trees Questions

Tree data structures as tested in coding interviews: binary trees, binary search trees, self-balancing trees (AVL insertion and deletion with rotations, a comparison with red-black trees, and alternatives such as treaps, splay trees and scapegoat trees), tries (prefix trees and radix trees), and N-ary, size-augmented and sum-augmented trees such as order-statistic trees. Covers traversal orders (in/pre/post-order, level-order, zigzag, iterative, generator-based and Morris variants), BST search, insertion and deletion invariants including duplicate-key policies, validation, successor and iterator design, range and kth-smallest queries, lowest common ancestor (including parent pointers, binary lifting and repeated queries on a static tree), tree reconstruction from traversals, serialization formats and parsing, converting between trees and sorted lists or linked lists, height, balance, diameter and path-sum recursion, subtree equality and hashing, persistent and snapshot-consistent trees, and how tree shape drives logarithmic versus degenerate performance. Excludes plain linked-list and stack/queue exercises with no tree in them, tree dynamic programming recurrences, general graph traversal, heap mechanics, and on-disk database index design, which are covered elsewhere.

MediumTechnical
42 practiced

Return all keys of a BST that fall inside [low, high] in sorted order, pruning subtrees that cannot contain answers. What is the cost in terms of the output size k and the height h?

EasyTechnical
44 practiced

What are the time and space costs of preorder, inorder, postorder and level-order traversal? Separate the recursion stack from explicit stack or queue usage, and show how the numbers change for balanced versus degenerate trees.

MediumTechnical
55 practiced

Flatten a binary tree into a linked list in place, following preorder, so each node's right child is the next node and left is null. What is the O(n) time, O(h) space version, and can you do it in O(1) extra space?

MediumTechnical
48 practiced

Find the kth smallest element in a BST. Use an iterative inorder walk that stops early, give the time and space cost, handle invalid k, and say how a Morris traversal would change the space cost.

EasyTechnical
46 practiced

Count the nodes, the leaves and the internal nodes of a binary tree, once recursively and once iteratively. State the base cases and the recursion depth on a very unbalanced tree.

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.