0% found this document useful (0 votes)
0 views

Must know tree structure in coding interviews

The document is a coding interview cheat sheet that outlines essential tree structures, including binary trees, binary search trees, heaps, n-ary trees, tries, and segment trees. Each structure is briefly described along with its properties and applications in coding interviews. The document emphasizes the importance of understanding these data structures for effective interview preparation.

Uploaded by

Mehul Thuletiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Must know tree structure in coding interviews

The document is a coding interview cheat sheet that outlines essential tree structures, including binary trees, binary search trees, heaps, n-ary trees, tries, and segment trees. Each structure is briefly described along with its properties and applications in coding interviews. The document emphasizes the importance of understanding these data structures for effective interview preparation.

Uploaded by

Mehul Thuletiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Coding Interview Cheat Sheet

Must know
tree
structures
in coding
interviews
5

8 ?

3 Begin coding interview prep -> neetcode.io


Binary Trees
A tree where each node can have at most two children. The children
nodes are on the left and right side, known as left child and right
child.

Each node may have both, one, or no children at all.

This is the most fundamental tree structure in coding interviews.

Node 3 has both

Node 2 has one

child.
2 3 children.

4 6 7

Node 6 has no

children - leaf.

Begin coding interview prep -> neetcode.io


Binary Search Tree
A binary search tree is a type of binary tree that follows the
binary search tree property. The left child’s value is always
less than the parent and the right child’s value is always
greater than the parent. 


In most implementations of BSTs, duplicates aren’t
allowed.

5
left child is smaller right child is greater
2 9
Valid BST

1 3

Not a valid BST

5
left child is greater right child is smaller
8 4

1 3
Begin coding interview prep -> neetcode.io
Heaps
A heap is a complete binary tree satisfying the min-heap or the
max-heap property. The value of the parent node is either
strictly greater or less than its children.

Heaps are used as priority queues in interviews, allowing us


to access the priority element in O(1) time.

i
A valid max-heap
100
left = 2 * i right = 2 * i + 1
19 36

17 3 25 1

2 7

A min heap would be the

opposite, where the root

node is the minimum.

Begin coding interview prep -> neetcode.io


n-ary Tree
These are trees where each node can have more than two
children - n children. These are used for file system hierarchies
and representing website structures.

The traversal of these needs to account for all of the children


rather than just left and right child.

3 2 4

5 6

Pre-order DFS on an n-ary tree.

Notice how we perform search

on all children, not just left and right.


Begin coding interview prep -> neetcode.io
Trie (Prefix Tree)
A trie is a tree data structure where each node represents a
single character, and paths from the root to nodes represent
words.

These are also called a prefix tree because each node stores a
prefix of a word.

Tries are useful for auto-complete features in a search engine.

P N X

P E

Each node can have up to 26 children

nodes.

Begin coding interview prep -> neetcode.io


Segment Tree
Segment trees are a data structure that allow for logarithmic
time querying and updates on intervals of an array.

Typically, these are good for finding range sum queries, or


finding the minimum and maximum value within a range.

0 1 2 3 4 5

5 3 7 1 4 2
0,5

0,2 3,5

3,4 5,5

0,1 2,2
3,3 4,4

Each node contains the sum for

0,0 1,1
a range of indices.

Begin coding interview prep -> neetcode.io


What other tree
should be on the list?

Comment below

neetcode.io

You might also like