Logo
Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Solution: Subtree of Another Tree

Problem Statement

Given two binary trees s and t, determine if tree t is a subtree of tree s. A tree t is considered a subtree of s if there exists a node in s such that the subtree of that node is identical to t. Both trees are considered identical if their structure and nodes are the same.

Examples

    • Input:
      • Tree s: [3,4,5,1,2]
      • Tree t: [4,1,2]
    • Expected Output: true
    • Justification: Tree t can be found as a subtree of s rooted at the node with value 4.
    • Input:
      • Tree s: [1,2,3]
      • Tree `t

.....

.....

.....

Like the course? Get enrolled and start learning!