Binary Tree Algorithms
- 30Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is a tree in computer science?
A tree contains nodes (drawn as circles) connected by edges (lines/arrows). Nodes can point to other nodes.
Or swipe the card
30 cards
What is a tree in computer science?
A tree contains nodes (drawn as circles) connected by edges (lines/arrows). Nodes can point to other nodes.
What terms are used to describe relationships between nodes in a tree?
Parent: a node that has children. Child: a node directly connected to its parent. Root: a node with no parent. Leaf: a node with no children.
What is the definition of a binary tree?
A binary tree is a tree where every node has at most two children (0, 1, or 2 children).
What are the three criteria that define a binary tree?
- Every node has at most two children.
- There is exactly one root (a node with no parent).
- There is exactly one path between the root and any node.
Is a tree with exactly one node considered a binary tree?
Yes, a singleton tree (one node) is a binary tree. It has one root, at most two children (zero), and exactly one path (to itself).
Is an empty tree (zero nodes) considered a binary tree?
Yes, an empty tree is considered a binary tree. This is a useful edge case for algorithms.
What is the root of a binary tree?
The root is the node that has no parent (no incoming edges). Typically drawn as the topmost node.
What is a leaf node in a binary tree?
A leaf node is a node that has no children (no outgoing edges).
In a binary tree, how are the two children typically referred to?
Left child and right child.
How do you represent a binary tree programmatically (e.g., in JavaScript)?
Using a Node class with properties:
val(the stored value),left(pointer to left child, default null), andright(pointer to right child, default null).What is a depth-first traversal of a binary tree?
A traversal that goes deeper in the tree before moving laterally. It visits nodes by going down a branch fully before backtracking.
What data structure is used to implement an iterative depth-first traversal?
A stack (LIFO - Last In, First Out).
In an iterative depth-first traversal, when do you add a node's value to the result list?
When the node is popped (removed) from the stack.
How do you ensure the correct left-to-right order in a depth-first traversal using a stack?
Push the right child first, then the left child. This ensures the left child is on top and visited next.
What are the time and space complexities of a depth-first traversal on a tree with n nodes?
Time: O(n). Space: O(n).
What is a breadth-first traversal of a binary tree?
A traversal that visits all nodes level by level, moving across the tree before going deeper.
What data structure is used to implement a breadth-first traversal?
A queue (FIFO - First In, First Out).
In an iterative breadth-first traversal, when do you add a node's value to the result list?
When the node is removed (dequeued) from the front of the queue.
Can a breadth-first traversal be easily implemented recursively?
No, it is not straightforward because recursion uses a stack (LIFO), while breadth-first requires a queue (FIFO).
What is the time complexity of a breadth-first traversal assuming an efficient queue?
O(n), where n is the number of nodes.
What is the problem 'tree includes'?
Given a binary tree and a target value, return true if the target value exists in the tree, else false.
How would you implement 'tree includes' using breadth-first search?
Use a queue initialized with the root. While queue not empty: dequeue a node; if its value equals target, return true; enqueue its existing children. If loop ends, return false.
How would you implement 'tree includes' recursively (depth-first)?
Base case: if root is null, return false. If root.val equals target, return true. Otherwise, recursively return treeIncludes(root.left, target) || treeIncludes(root.right, target).
What is the problem 'tree sum'?
Given a binary tree with numeric values, return the total sum of all node values.
How would you compute 'tree sum' recursively?
Base case: if root is null, return 0. Otherwise, return root.val + treeSum(root.left) + treeSum(root.right).
What is the problem 'tree min value'?
Given a non-empty binary tree with numeric values, return the smallest value in the tree.
How would you solve 'tree min value' recursively?
Base case: if root is null, return Infinity. Otherwise, return Math.min(root.val, treeMin(root.left), treeMin(root.right)).
What is the problem 'max root-to-leaf path sum'?
Given a binary tree with numeric values, return the maximum sum of values along any path from the root to a leaf.
What is the recursive algorithm for 'max root-to-leaf path sum'?
Base cases: if root is null, return -Infinity; if root is a leaf (no children), return root.val. Otherwise, return root.val + Math.max(maxPathSum(root.left), maxPathSum(root.right)).
Why is -Infinity used as the base case for null nodes in max path sum?
Because we want to take the maximum; -Infinity is never larger than any actual number, so it won't be selected.

Ask anything
We've read this whole study set, so questions about it get the best answers.