Subtree of Another Tree

easyTreesDFS + same-tree check at each nodeB75Solve on LeetCode#572

Part of Practice: Recursive Foundations

You are given the roots of two binary trees, root and subRoot. Return true if some node inside root, together with all of its descendants, forms a tree identical to subRoot — same shape and same values. A tree counts as a subtree of itself.

root has 1 to 2,000 nodes and subRoot has 1 to 1,000 nodes. Note the match must include all descendants: you cannot stop partway down a branch.

Examples

Input:
root = [3,4,5,1,2], subRoot = [4,1,2]
Output:
true

The subtree hanging at the node 4 is exactly [4,1,2].

Input:
root = [3,4,5,1,2,null,null,null,null,0], subRoot = [4,1,2]
Output:
false

The node 4 in root now has an extra descendant 0 under its child 2, so its full subtree is [4,1,2,null,null,0], not [4,1,2].

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.