109 trees interview questions, asked at Google, Amazon, Meta and 19 more companies. Last asked Nov 2026.
+2
Binary tree right side viewCodingReturn the values you would see looking at a binary tree from its right side: the last node of each level, from top to bottom.
Last asked Nov 2026
+1
Serialize and deserialize a binary treeCodingWrite two functions for a binary tree of integer values: encode turns a tree into a string, and decode rebuilds a tree with exactly the same structure and values from that…
Sum of distances from every node in a treeCodingYou get an undirected tree with n nodes labelled 0..n-1 as a list of n - 1 edges. For every node, return the sum of its distances (in edges) to all other nodes.
Last asked Oct 2026
+1
Validate a binary search treeCodingDecide whether a binary tree is a valid binary search tree: for every node, every value in its left subtree is strictly smaller and every value in its right subtree is strictly…
Decide whether two people in a family tree are relatedCodingYou get a family tree as parent-child links. Two people are related when they share at least one common ancestor, where each person counts as their own ancestor (so a parent and…
Rebuild a task tree from parent-child records and print itCodingYou get a list of task records and must rebuild the hierarchy of tasks and subtasks they describe, then print it as a text tree. Root records have the fields timestamp, task…
Lowest common manager in an org chartCodingA company's reporting structure is a rooted tree. Each person is an Employee with a unique name and a list reports of the people who report to them directly.
Last asked Oct 2026
Build reporting trees from employee-manager pairsCodingYou get a list of reporting relationships. Each person is a positive integer id, and a pair [employee, manager] means the employee reports directly to that manager.
Last asked Sep 2026
Binary search tree keys within a rangeCodingGiven a binary search tree and an inclusive range [low, high], return every key in the tree that lies in the range, in ascending order. Use the search tree's ordering so you skip…
Customer revenue with referral credit and bottom-k queryCodingBuild an in-memory system that tracks how much revenue each customer brings in, including revenue from the customers they referred. Customers get integer ids starting at 0, in…
Last asked Sep 2026
+1
Unix find command with pluggable filtersCodingImplement a small version of the Unix find command over an in-memory file system. The file system holds directories and files.
Last asked Sep 2026
Delete leaves with a target value, repeatedlyCodingGiven a binary tree and a target value, delete every leaf that holds the target. Deleting a leaf can turn its parent into a leaf; if that parent also holds the target, delete it…
Last asked Sep 2026
Lowest common ancestor in a binary search treeCodingGiven a binary search tree and two distinct keys p and q that are both in it, return their lowest common ancestor: the node farthest from the root whose subtree contains both. A…
Last asked Sep 2026
Binary tree vertical order traversalCodingReturn a binary tree's values column by column, from the leftmost column to the rightmost. The root is at column 0, row 0.
Last asked Sep 2026
Range sum of a binary search treeCodingGiven a binary search tree with distinct values and two bounds low and high, return the sum of every node value v with low <= v <= high. Skip subtrees that can't contain any…
Last asked Sep 2026
+1
Lowest common ancestor of a binary treeCodingGiven a binary tree whose values have no ordering, and two distinct nodes in it, return their lowest common ancestor: the deepest node that has both nodes in its subtree. A node…
Rewrite a SQL query plan treeCodingA query engine represents a SQL query as a tree of plan nodes. You are handed such a tree and asked to make it cheaper without changing the rows it returns.
Last asked Sep 2026
+1
Count and map a cluster tree with message passingCodingA cluster of n machines is wired as a rooted tree (any number of children per node). Each machine knows only three things: its own id (a string such as "4"), its parent's id…
Last asked Sep 2026
Filter an n-ary tree by substring and render it indentedCodingEach node of an n-ary tree holds a string value and a list of children. First write a function that removes every node whose value contains a given substring, together with its…
Last asked Sep 2026
Simplify an arithmetic expression treeCodingYou get the root of an expression tree. Every internal node holds a binary operator (+, - or ) and has exactly two children; every leaf holds an integer constant or a variable…
Last asked Sep 2026
Throne inheritance without an initial kingCodingModel a royal family's line of succession. People are born to a parent, people die, and you can ask for the current order of inheritance: a preorder walk of the family tree…
Parse HTML into an element tree and query itCodingYou are asked to "write an HTML parser". The prompt is deliberately vague: it does not say what the input is, what to return or which edge cases matter.
Kth smallest value in a binary search treeCodingGiven a binary search tree and a number k, return the kth smallest value in the tree. k is one-based: k = 1 is the minimum.
Last asked Aug 2026
Format a forest of nested commentsCodingYou get a list of comments in no particular order. Each comment has an id, the id of the comment it replies to (or none for a top-level comment), and its text.
Last asked Aug 2026
Print each tree node's level and whether it is balancedCodingYou get a binary tree stored as a zero-based level-order array: the children of index i are at 2i + 1 and 2i + 2, and None marks a missing node. For every node, report its level…
Last asked Aug 2026
Sum root-to-leaf numbers in a digit treeCodingEach node of a binary tree holds a digit from 0 to 9, so every path from the root to a leaf spells a number (root digit first). Return the sum of all those numbers.
Last asked Aug 2026
Serialize and deserialize an N-ary treeCodingIn a rooted N-ary tree each node has any number of children, kept in a fixed order. Design a string format for such trees and implement both directions: serialize turns a tree…
Tree class with depth-first and breadth-first operationsOO designDesign a tree class and implement a set of operations on it. Some are naturally depth-first and some breadth-first: implement each with the traversal that suits it, and explain…
Last asked Jul 2026
+2
Vertical order traversal of a binary treeCodingGive every node of a binary tree a position: the root is at row 0, column 0; a node's left child is one row down and one column left, its right child one row down and one column…
Last asked Jun 2026
+1
Build a quadtree over a square gridCodingA quadtree splits a square region of the plane into four quadrants, recursively: every node is either a leaf or has exactly four children. Build one yourself, in two versions.
Last asked Jun 2026
Lowest common ancestor in a BST and in a binary treeCodingGiven the root of a tree and two values that occur in it, return the value of their lowest common ancestor: the deepest node that has both in its subtree (a node counts as its…
Last asked Jun 2026
Kth largest perfect binary subtreeCodingIn a binary tree, a subtree (a node and all its descendants) is perfect when every internal node has two children and all leaves are on the same level, so it has 2^h - 1 nodes…
Team with the highest average salary in an org chartCodingAn org chart is a tree of employees, each with a salary and a manager (the CEO has none). A manager's team is the manager plus everyone below them, directly or indirectly.
Last asked Jun 2026
Longest path of equal-valued tree nodesCodingGiven the root of a binary tree, return the length of the longest path in which every node has the same value. The path may start and end at any nodes and does not have to pass…
Nested todo list with subtasksCodingBuild an in-memory todo list where any task can hold subtasks, nested to any depth. Users add tasks, complete them and print the list with its hierarchy.
Last asked Jun 2026
+1
Convert a BST to a sorted circular doubly linked listCodingRearrange a binary search tree, in place, into a circular doubly linked list in sorted order. Reuse each node's left pointer as "previous" and right as "next". The last node's…
Last asked Jun 2026
+1
All nodes at distance K in a binary treeCodingGiven the root of a binary tree, a target node in it and an integer k, return the values of every node exactly k edges away from the target. Edges count in both directions, so a…
Last asked Jun 2026
Build a weighted binary tree from character countsCodingGiven a string s of letters ('A'-'Z', 'a'-'z'; case matters), build a binary tree that summarizes how often each character appears. Each node has three fields: ch, count, and two…
Last asked Jun 2026
Pick the root that needs the fewest edge reversalsCodingYou are given a tree with n nodes labeled 1 to n. Its n - 1 edges are directed: each edge (u, v) points from u to v. Ignoring direction, the edges connect all nodes into a tree…
Last asked May 2026
Validate a parent-child forestCodingGiven a list of parent-child pairs, decide whether they describe a valid forest: a set of rooted trees where every node has at most one parent, there are no cycles, and each tree…
Last asked May 2026
Palindromic paths from a node to the rootCodingA storage system keeps its directories in a tree. Each directory holds one lowercase letter as a label. You are given: - arr: a list of n lowercase letters; arri is the label of…
Last asked May 2026
+1
Diameter of a binary treeCodingReturn the number of edges on the longest path between any two nodes of a binary tree. The path doesn't have to pass through the root. Solve it in one pass over the tree. Unless…
Last asked May 2026
Merge two named treesCodingEach tree node is a dictionary with three keys: name (a string), value (an integer) and children (an ordered list of nodes). Given two such trees t1 and t2, build a new merged…
Last asked May 2026
Flower placement and directory deletionCodingTwo short, independent problems in one round. A row of plots is given as a list of 0s (empty) and 1s (already planted). No two flowers may be in adjacent plots. Decide whether n…
Last asked May 2026
Longest consecutive sequence path in a binary treeCodingGiven the root of a binary tree, return the length (in nodes) of the longest downward path, from a parent to a child and so on, in which each value is exactly one greater than the…
Last asked Apr 2026
Everyone under a manager in an org chartCodingA company's org chart is a tree: every employee except the top one has one manager. Given the chart and one manager, walk it depth-first and return everyone in that manager's…
Binary tree zigzag level-order traversalCodingGiven a binary tree, return its node values level by level, alternating direction: the first level left to right, the second right to left, the third left to right, and so on…
Last asked Apr 2026
Siblings, cousins or neither: two nodes in a treeCodingGiven a rooted tree (each node can have any number of children) and the values of two nodes, say how they are related: "siblings" if they have the same parent, "cousins" if they…
Build a binary tree from parent-child descriptionsCodingEach description is parent, child, isleft: child is the left child of parent when isleft is 1, the right child when it is 0. The descriptions together describe one binary tree…
Last asked Mar 2026
List every file path in a directory treeCodingYou get an in-memory file system as a tree. Each node has a name, an isFile flag and a list of children (empty for files). Return the full path of every file, joining names with /…
Last asked Mar 2026
Construct a BST from its preorder traversalCodingYou get the preorder traversal of a binary search tree whose values are all distinct. Rebuild the tree and return its root. Aim for linear time. Unless you agree on a different…
Last asked Mar 2026
Serialize an expression tree with the fewest parenthesesCodingGiven a binary expression tree whose leaves are numbers or variables and whose internal nodes are the operators +, -, and /, print it as an infix expression with only the…
Last asked Feb 2026
Kth smallest element in a BSTCodingGiven the root of a binary search tree and an integer k, return the k-th smallest value in the tree, counting from 1. In a binary search tree every value in a node's left subtree…
Last asked Feb 2026
Manager and peer relationships with is_manager queriesCodingBuild an in-memory org chart that learns relationships between employees one statement at a time and answers whether one employee is above another. Statements and questions arrive…
Last asked Feb 2026
Populate next right pointers in each nodeCodingYou get the root of a binary tree. Each node has val, left, right and a next pointer that starts as null. Set every node's next to the node immediately to its right on the same…
Check whether a binary tree is completeCodingGiven the root of a binary tree, return whether it is complete. A complete binary tree has every level full except possibly the last, and the nodes of the last level are as far…
Last asked Feb 2026
Cheapest common ancestor in a treeCodingEvery node of a rooted tree has a cost. Given a set of nodes, return the common ancestor of all of them with the lowest cost. A node counts as an ancestor of itself. Unless you…
Last asked Feb 2026
Lowest common ancestor with parent pointersCodingEach node of a binary tree has value, left, right and parent fields. You are given references to two nodes p and q of the same tree, but not the root. Return their lowest common…
Last asked Feb 2026
Lazy in-order BST iteratorCodingWrite an iterator over a binary search tree that returns its values in sorted order, one at a time. It must be lazy: don't copy the tree into a list. Memory should be proportional…
Last asked Feb 2026
+2
Minimum distance between two nodes in an N-ary treeCodingYou are given the root of an N-ary tree. Every node has an integer val, unique within the tree, and a list children of its direct children. There are no parent pointers…
Last asked Feb 2026
Check that a parent array forms a valid treeCodingYou get an array parent of length n, where parenti is the parent of node i and -1 marks a root. Return whether the array describes one valid rooted tree: exactly one root, no…
Last asked Feb 2026
Symmetric tree: is a binary tree its own mirrorCodingGiven the root of a binary tree, return whether the tree is a mirror image of itself around its centre. Unless you agree on a different interface with the interviewer, assume: -…
Last asked Feb 2026
Populate next pointers in a perfect binary treeCodingYou are given a perfect binary tree: every internal node has two children and all leaves are on the same level. Each node has left, right and next. Set every node's next to the…
Last asked Feb 2026
Maximum path sum in an N-ary treeCodingEach node of an N-ary tree holds an integer, which may be negative, and has any number of children. A path is a sequence of nodes joined by parent–child edges that visits each…
Last asked Jan 2026
Time for an infection to spread through a binary treeCodingA binary tree catches fire at one node at minute 0. Every minute, the fire spreads from each burning node to its left child, its right child and its parent. Return how many…
Last asked Jan 2026
Binary search tree iteratorCodingImplement an iterator over a binary search tree that returns its values in ascending order. next() returns the next smallest value and hasnext() says whether one remains. Each…
Resolve user roles across an account hierarchyCodingAccounts form a tree: an organization has sub-accounts, which can have their own sub-accounts. A user can be granted roles on any account, and a role granted on an account also…
Last asked Jan 2026
Build a binary tree from a parenthesized stringCodingA string encodes a binary tree: an integer (possibly negative) followed by zero, one or two subtrees, each in parentheses and encoded the same way. The first pair of parentheses…
Last asked Jan 2026
Binary tree level order traversal IICodingGiven the root of a binary tree, return its values level by level from the bottom up: the deepest level first and the root last. Within each level, list the nodes from left to…
Last asked Dec 2025
All root-to-leaf path sumsCodingGiven the root of a binary tree whose nodes hold integers (possibly negative), return a list with the sum of the values along every path from the root to a leaf. A leaf is a node…
Last asked Dec 2025
Lowest common ancestor in an N-ary treeCodingGiven the root of a rooted N-ary tree (each node has any number of children) and two distinct nodes p and q in it, return their lowest common ancestor: the deepest node that has…
Last asked Dec 2025
Mode of a ternary search treeCodingEach node of this tree has up to three children: left holds values smaller than the node's, middle holds values equal to it, and right holds values larger. Return the mode: the…
Last asked Nov 2025
Outer boundary of an N-ary treeCodingYou get the root of an N-ary tree whose children are ordered from left to right. Group the nodes by depth, with the root at depth 0. At each depth, the leftmost node is the first…
Last asked Oct 2025
Print a directory tree with indentationCodingYou get the root of a file system tree. Each node is a directory, which can have children, or a file, which has none. Print the whole tree starting from the root, one entry per…
Last asked Oct 2025
Store words for lexicographic range queriesSystem designDesign storage for a persistent set of words that answers range queries: given low, high, inclusive, return every stored word w with low <= w <= high, in dictionary order. Explain…
Last asked Oct 2025
Is a sequence a subsequence of a tree's in-order traversal?CodingYou get a binary tree of integers and an array target. Decide whether target is a subsequence of the tree's in-order traversal: its values appear in that order, not necessarily…
Last asked Sep 2025
+1
Left and right views of a binary treeCodingGiven a binary tree, return its left view and its right view. The left view lists, for every depth from the root down, the leftmost node at that depth. The right view lists the…
Last asked Sep 2025
Unique visitors per department from click logsCodingAn online store has two tables. products(productid, department, category, subcategory) places every product in a hierarchy: a department contains categories, which contain…
Last asked Sep 2025
Root-started value path in an N-ary treeCodingYou are given the root of an N-ary tree (each node has an integer val and a list of children) and a list of integers path. Return true if there is a downward path that starts at…
Last asked Sep 2025
Remove deleted folders and their descendants from a path listCodingYou get a list of hierarchical paths separated by /, such as California, California/San Francisco and California/San Francisco/7th Street, and a list of paths to delete. Deleting…
Last asked Sep 2025
Subsets, with and without backtrackingCodingGiven an array of distinct integers, return all its subsets (the power set), in any order. Write it twice: once with backtracking, and once iteratively with loops only and no…
Last asked Sep 2025
Sum tree with node values computed from childrenCodingYou get a rooted tree. Each leaf holds an integer. Every internal node's value is the sum of its children's values, so it equals the sum of the leaves below it. Answer value…
Last asked Sep 2025
Implement a trie (prefix tree)CodingImplement a prefix tree over strings. Besides insert and lookup it counts words by prefix and supports deleting a word. The same word can be inserted more than once. Design the…
Last asked Aug 2025
Lock, unlock and upgrade nodes in a treeCodingA rooted tree of N nodes labeled 0..N-1 is given by a parent array: parent0 = -1 and parenti is the parent of node i. Build a lock manager over it. The rules come in two parts;…
Last asked Aug 2025
Block tree with add, delete subtree and renderCodingA document is an ordered forest of blocks. Each block has an id and some text, and it is either top level or the child of another block. Process a list of operations that add…
Last asked Jul 2025
Delete a node from a binary search treeCodingGiven the root of a binary search tree with unique values and a key, delete the node holding the key if it exists and return the root of the resulting tree, which must still be a…
Last asked Apr 2025
Design a folder hierarchy for workspace filesSystem designA team workspace keeps file metadata in nested folders. Members list a folder, create files and folders, rename them, and move a file or a whole folder to another parent. Design…
Last asked Jan 2021
Folder path from a node idCodingA file browser stores its folder hierarchy as three parallel arrays: - idListi is a unique folder id. - childreni lists the ids of the folders directly inside folder idListi. -…
Peel an N-ary tree one leaf layer per dayCodingEvery day, all leaves of an N-ary tree fall off at the same time. A leaf is a node with no children left. When a node loses its last child, it becomes a leaf and falls on the next…
Merge two n-ary trees by child positionCodingImplement mergetrees(root1, root2). It takes the roots of two n-ary trees and returns the root of a new tree that merges them. Each node has an integer val and an ordered list…
+1
Root of the largest tree in a forestCodingYou are given a list relations. Each entry child, parent says that parent is the direct parent of child. Together the relations form a forest: there are no cycles, and every node…
+2
Compress an n-ary tree by promoting childrenCodingEach node of an n-ary tree holds a unique integer value and an ordered list of children. You are given the root and a list compressednodes of values. Implement compress(root…