A design-system audit looks for repeated pieces of UI that could become shared components. The page is a binary tree of component ids. Two subtrees are duplicates when they have the same shape and the same id in every position.
Write countRepeatedShapes(root) that returns how many different subtree shapes appear two or more times in the tree. Each shape counts once, however often it repeats. A subtree is a node with everything below it.
Input: root = [1,2,3,4,null,2,4,null,null,4] Output: 2
The subtree 2 → 4 appears twice, and the leaf 4 appears three times.
Input: root = [2,1,1] Output: 1
The leaf 1 appears twice.
0 <= number of nodes <= 5000-200 <= id <= 200Trees are written level by level, like [1, 2, 3, null, 4], where null is a missing child. Your function works with TreeNode objects, each with val, left, and right.
Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #364 Repeated Component Subtrees. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.