Misc
114. Flatten Binary Tree to Linked List
Medium·
4 Approachesclick to switch
1
Sentinel + Preorder
O(n)
O(h)
2
Recursive with Tail Return
O(n)
O(h)
3
Iterative Stack
O(n)
O(h)
4
Morris-like (Space Optimized)
O(n)
O(1)
FIG. FLATTEN BINARY TREE TO LINKED LIST● INTERACTIVE
visualization loads as you reach it
- Time
- O(n)
recvisits each of thennodes exactly once, rewiringcurr.rightand recursing intoleftandright.- Space
- O(h)
- The recursion stack holds one frame per level, so it grows to the tree's height
h(up tonfor a fully skewed tree).
234. Palindrome Linked List
Easy·
3 Approachesclick to switch
1
Array Approach
O(3n)
O(2n)
2
Recursive Approach
O(n)
O(n)
3
Two Pointers with Reversal (Optimal)
O(2n)
O(1)
FIG. PALINDROME LINKED LIST 2● INTERACTIVE
visualization loads as you reach it
- Time
- O(3n)
- One
O(n)pass to buildarr, oneO(n)pass forarr[::-1]to build the reversed copy, and oneO(n)pass for the==comparison -3n, wherenis the number of nodes. - Space
- O(2n)
arrholds allnnode values, andarr[::-1]allocates a secondn-length list -2n.