Properties of a Tree
• Tree is a graph where:
– n-1 edges
– No cycles
• If two of the above are true, then all 3 are
• Tree questions are usually asked to test your
understanding of recursion.
Binary Search Trees
• Binary tree is one where each node as at most
• Binary Search tree is a binary tree where all
the descendants to the left of a node are
smaller than it and all the descendants to the
right are greater than it.
• Most interview questions focus on binary or
binary search trees.
• (Usually) binary tree where each node’s
children are smaller (or larger in a Min-heap)
• Allows for constant time extraction of the
largest element in the tree.
• Lookup becomes O(n)
• Used if extracting the smallest or largest node
• Searches minimum
depths first, always
searches lowest depth
• High memory
• Shortest path
• Explores full depth,
doesn’t search any
particular depth last
• Low memory
– Explore node, then left children, then right
– Explore left children, then node, then right
– Explore left children, then right, then node
• Implemented using simple recursion
Find the height of a binary tree.
• Use recursion
• Think of each child as its own separate subtree
with that node as the root
• Return 1 + the max of left subtree’s height and
right subtree’s height
Given the value of two nodes in a BST, find the
lowest common ancestor. You may assume both
values exist in the tree.
• Uses the left/right descendant property of a
BST to traverse through the nodes.
• Starting at the root, traverse through the tree.
• If both values are greater than the current
node, go right.
• If both are smaller than the current, go left.
• LCA will be the first node you encounter that
is between the two nodes you are given.
Other Common Tree Questions
• Binary tree to heap
• Is Binary tree a BST?
• Balance an unbalanced BST (left or right
• Count # of leaves
• Graph questions are much more complicated
and less common.
• We will cover graphs during the group