Zigzag Level Order Traversal — BFS with Deque in Python
Zigzag level order traversal of a binary tree using BFS and deque. Step-by-step Python solution with visual trace, complexity analysis, and interview tips.
33 posts · page 1 of 1
Zigzag level order traversal of a binary tree using BFS and deque. Step-by-step Python solution with visual trace, complexity analysis, and interview tips.
Learn how to flatten a binary tree to a linked list using preorder threading, Morris traversal, and how to convert a BST to a sorted doubly linked list — with full Python implementations and Big-O analysis.
Master BST iterator using stack-based controlled in-order traversal, range sum queries, counting nodes in range, closest value, and closest K values — with full Python implementations.
Complete guide to boundary traversal — left boundary, leaf nodes, and right boundary in reverse. Multiple Python approaches with edge case handling.
Solve distance problems in binary trees — distance between two nodes, all nodes at distance K, burning a tree from a node, and sum of distances using rerooting. Full Python implementations.
The complete guide to binary tree interview patterns — top 20 patterns, DFS vs BFS decision guide, recursive vs iterative approaches, common mistakes, complexity cheatsheet, and template code.
Master tree pruning and deletion — delete nodes in BST, prune binary trees, trim BST to range, and remove leaves with a given value. Full Python implementations with Big-O analysis.
Solve all Path Sum variants — root-to-leaf existence, find all paths, any-to-any with prefix sums. Complete Python solutions with Big-O analysis.
Check if a tree is symmetric, whether one tree is a subtree of another, same tree comparison, and flip equivalence. Python solutions with analysis.
Master vertical order traversal with column-based grouping. Includes top view, bottom view, and vertical sum with Python solutions.
Master zigzag level-order traversal of binary trees using deque and flag toggling. Multiple Python approaches with step-by-step walkthrough.
Understand AVL trees — balance factors, all four rotation types (LL, RR, LR, RL), insertion with rebalancing, and complete Python implementation with height tracking.
Convert sorted arrays and linked lists into balanced BSTs using divide and conquer. BST to sorted DLL, Day-Stout-Warren algorithm, and Python implementations.
Master binary lifting to answer Lowest Common Ancestor (LCA) and kth ancestor queries in O(log n) with O(n log n) preprocessing. Full Python implementations with tree examples.
Solve all binary tree view problems — left view, right view, top view, bottom view, and vertical order traversal. BFS-based Python implementations.
Master Binary Search Tree operations — insert, search, and all three delete cases with in-order successor. Full Python implementation with time complexity analysis.
Master LCA problems — recursive DFS for binary trees, BST property shortcut, parent pointers, and binary lifting for O(log n) queries. Full Python code.
Master N-ary tree traversals and Trie data structure — autocomplete, spell check, word dictionary, wildcard search. Complete Python implementations.
Understand Red-Black trees — the 5 rules, insertion cases, color flips, rotations, comparison with AVL, and why Java TreeMap and Linux use them.
Build binary trees from inorder + preorder, inorder + postorder, and preorder + postorder. Hashmap optimization, edge cases, and Python recursive solutions.
Find the diameter of a binary tree using DFS, solve maximum path sum, and understand the two-BFS approach for general trees. Full Python implementations.
Serialize binary trees to strings and deserialize them back. BFS and DFS approaches with null handling, Python implementations, and real-world uses.
Deep dive into advanced tree algorithms — heavy-light decomposition, Euler tour technique, centroid decomposition, LCA with binary lifting, tree DP with rerooting, and virtual trees.
Master every tree traversal method with recursive, iterative, and Morris traversal implementations, plus classic LeetCode tree problems.
The queue-with-size BFS pattern, why DFS still works, and how this template extends to zigzag and right-side view.
The inorder traversal trick, the iterative early-exit version, and the follow-up that haunts FAANG interviews.
Why BST ordering collapses LCA into a one-line traversal, and the iterative version that needs zero extra space.
The DFS-with-decrement trick, why the leaf condition is the whole problem, and the variants that build on it.
The preorder-with-nulls encoding, the iterator-driven decoder, and why level-order is a worse choice than it looks.
The seductive wrong answer, the correct min/max bounds approach, and how to defend it in an interview.
A practical introduction to binary trees — the TreeNode class, terminology (full, complete, perfect, balanced), height vs depth, BSTs, and the small calculations you need to reason about tree problems.
A practical guide to the four canonical binary tree traversals — recursive and iterative versions, when to use each, and the patterns that make them click.
Eight classic binary tree interview problems with examples, approach notes, and clean Python solutions — max depth, same tree, invert, symmetric, path sum, LCA, validate BST, and serialize/deserialize.