Subtree of Another Tree Visualizer & Step-by-Step Algorithm Solution

Check if binary tree subRoot is a subtree of root with identical structure and node values.

Category: trees | Difficulty: Easy

Tags: DFS, Binary Tree, Recursion, Tree Comparison, Subtree

Subtree of Another Tree

3
4
1
2
5
4
1
2
100%
state
resultchecking
Initialization
1/14
Explanation

Start isSubtree: checking if subRoot tree is a subtree of root.

Source Code
1function isSubtree(root, subRoot) {
2 if (!root) return false;
3 if (isSameTree(root, subRoot)) return true;
4 return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
5}