@takeUforward_@striver_79 (9/9)
- for every level, width = last index - first index + 1, using indices relative to the leftmost node in that level (to avoid overflow from ever-growing numbers). track the max across all levels.
Day 35 #SDESheetChallenge
4 problems.
- pre, in, post order in a single traversal
- vertical order traversal
- root to leaf paths
- maximum width of a binary tree
learnings & explanation thread (1/9)
@takeUforward_
@takeUforward_@striver_79 (8/9)
Maximum Width of a Binary Tree -
- find the widest level, counting the gap between leftmost and rightmost node even if nodes are missing in between.
- assign each node an index, root is 0, left child is 2×index+1, right child is 2×index+2. heap-style indexing.
@takeUforward_@striver_79 (10/10)
Top View of a Binary Tree –
- same line-based bfs as previous, but flipped: only insert into the map if that line is still empty.
- the first node to reach a line is always the highest one there, so it claims the spot and nothing after can overwrite it.
Day 34 #SDESheetChallenge
3 problems.
- morris preorder traversal
- left/right view
- bottom/top view
learned that a tree can be given horizontal coordinates. left -1, right +1, and that alone solved top/bottom view of BT today.
thread (1/10)
@takeUforward_
@takeUforward_@striver_79 (9/10)
- every time a node lands on a line that's already occupied, overwrite it. since bfs processes top to bottom, deeper nodes overwrite shallower ones, leaving only the lowest node per line. O(N log N).
(9/9)
- by the end, the tree looks untouched. but for a moment, it was quietly rewired to remember your path.
- empty right pointers become temporary links back to the parent. once you use one to climb back, you erase it. O(N) time, true O(1) space.
Day 33 #SDESheetChallenge
trees, day 1. 4 traversal methods, diff way to visit the same nodes. one of 'em barely uses any memory.
- inorder traversal
- preorder traversal
- postorder traversal
- morris inorder traversal
learnings and explanation in thread (1/9)
@takeUforward_
(8/9)
- eventually you'll follow that thread back up. you'll know because the predecessor's right pointer already points to cur.
- remove the thread, process cur, then move right.