Find Duplicate Subtrees — Serialize + HashMap
Find all duplicate subtrees by serializing each subtree and tracking serialization counts in a hash map.
webcoderspeed.com
26 articles
Find all duplicate subtrees by serializing each subtree and tracking serialization counts in a hash map.
Flatten a binary tree in-place into a linked list following pre-order using the Morris traversal approach.
Serialize a binary tree to a string using BFS level-order and deserialize back using a queue for reconstruction.
Master every tree pattern: DFS traversals, BFS level-order, BST operations, LCA, path sum, and serialization — with templates and full index of 75 problems.
Find the maximum depth of a binary tree using recursive DFS (one line) or iterative BFS level counting.
Invert a binary tree by recursively swapping left and right children at every node.
Check if a binary tree is symmetric by comparing mirror subtrees with a two-pointer recursive approach.
Determine if a root-to-leaf path exists with a given sum using DFS that subtracts each node value from the target.
Check if two binary trees are identical by recursively comparing structure and node values.
Check if a binary tree is height-balanced by computing subtree heights and returning -1 early on imbalance.
Merge two binary trees by adding overlapping node values recursively, using existing nodes where possible.
Sum all BST values in [low, high] using BST property to prune entire subtrees outside the range.
Find a node with given value in a BST by navigating left or right based on the BST property.
Return values grouped by level using BFS with a queue, processing each level in a batch loop.
Traverse a binary tree in zigzag level order by toggling insertion direction at each level.
Find the rightmost node at each level using BFS and taking the last element seen at each depth.
Collect all root-to-leaf paths with a given sum using DFS backtracking, appending and removing the current node.
Find the diameter (longest path between any two nodes) using a depth function that tracks the maximum path through each node.
Validate a BST by passing down min and max bounds through DFS, ensuring every node falls strictly within its valid range.
Find the kth smallest element in a BST using inorder traversal (left-root-right) which visits nodes in sorted order.
Find the LCA of two nodes using post-order DFS: return root when found, propagate upward and split at the ancestor.
Find the LCA in a BST in O(h) by navigating left or right based on whether both nodes are in the same subtree.
Build a binary tree from preorder and inorder traversals by using the preorder root and inorder index to split left/right subtrees.
Count paths in a binary tree summing to target (not necessarily root-to-leaf) using a prefix sum frequency map.
Serialize a binary tree to a string and deserialize it back. Meta loves this problem for testing tree traversal and string parsing. Both BFS and preorder DFS approaches shown.
Return the values visible from the right side of a binary tree — the last node of each BFS level. O(n) BFS with level tracking.