Trees Flashcards

(15 cards)

1
Q

LeetCode 226 – Invert Binary Tree
Swap every node’s left/right.

A

Recursively or iteratively swap left ↔ right for each node.

DFS (pre/post) or BFS works.

Time O(n), Space O(h) (recursion) or O(w) (queue).

Tags: Tree, DFS/BFS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

LeetCode 104 – Maximum Depth
Return height of tree.

A

Depth = 1 + max(depth(left), depth(right)).

Or BFS level count.

O(n) time, O(h) space.

Tags: Recursion, BFS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

LeetCode 543 – Diameter
Longest path between any two nodes (#edges).

A

DFS returns height; update global best = max(best, leftH + rightH).

Answer is best.

O(n) time, O(h) space.

Tags: DFS, Height.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

LeetCode 110 – Balanced?
|height(L) − height(R)| ≤ 1 for all nodes.

A

Postorder: get heights; if any subtree unbalanced, bubble up sentinel (e.g., -1).

O(n) time, O(h) space.

Tags: DFS, Pruning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

LeetCode 572 – Subtree of Another Tree
Is t a subtree of s?

A

Traverse s; when node value == root(t), run isSameTree.

Short-circuit on match.

O(n·m) worst; average faster with pruning or hashes.

Tags: DFS, Matching.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

LeetCode 235 – LCA in BST
Find LCA(p, q).

A

Use BST order:

If both < root → go left; both > root → right; else root is LCA.

Iterative or recursive.

O(h) time, O(1)/O(h) space.

Tags: BST properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

LeetCode 102 – Level Order
Nodes by level (left→right).

A

BFS queue; process current level size, push children.

Collect list per level.

O(n) time, O(w) space.

Tags: BFS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

LeetCode 199 – Right Side View
One node per level as seen from right.

A

BFS: record the last node popped per level.

Or DFS right-first, keep first visit at each depth.

O(n) time, O(w)/O(h) space.

Tags: BFS/DFS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

LeetCode 100 – Same Tree
Structural + value equality?

A

Recursively compare: both null → true; one null → false; vals equal & left/same & right/same.

O(n) time, O(h) space.

Tags: DFS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

LeetCode 1448 – Count Good Nodes
Node is “good” if ≥ max on path root→node.

A

DFS carry maxSoFar; count if val ≥ maxSoFar.

Update maxSoFar = max(maxSoFar, val).

O(n) time, O(h) space.

Tags: DFS with state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

LeetCode 98 – Validate BST
Is tree strictly BST?

A

Method 1: DFS with (low, high) bounds; node.val must be (low, high).

Method 2: Inorder traversal must be strictly increasing.

O(n) time, O(h) space.

Tags: BST, Inorder, Bounds.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

LeetCode 230 – Kth Smallest in BST

A

Inorder traversal yields sorted order; decrement k until 0.

Iterative stack to avoid recursion.

(Follow-ups: maintain subtree sizes for updates.)

O(h + k) time, O(h) space.

Tags: BST, Inorder.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

LeetCode 105 – Build Tree (Preorder + Inorder)

A

Preorder gives root order; inorder splits left/right.

Map val → inorderIndex for O(1) splits.

Recurse with index ranges; advance preorder pointer.

O(n) time, O(n) space.

Tags: Recursion, Array indices.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

LeetCode 124 – Max Path Sum
Path can start/end anywhere; at most one turn at a node.

A

DFS returns max gain to parent: max(0, max(leftGain, rightGain)) + val.

Update global: best = max(best, leftGain + rightGain + val).

Handle negatives.

O(n) time, O(h) space.

Tags: DP on Trees.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

LeetCode 297 – Serialize/Deserialize
Convert between tree and string.

A

Common: BFS with null markers (e.g., #) and commas.

Serialize: level order append;

Deserialize: rebuild by reading queue and attaching children.

Alternatively: Preorder with sentinel + index.

O(n) time, O(n) space.

Tags: BFS/Preorder, Encoding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly