Inverted Subtree - Google Top Interview Questions


Problem Statement :


A tree is defined to be an inversion of another tree if either:

Both trees are null
Its left and right children are optionally swapped and its left and right subtrees are inversions.
Given binary trees source and target, return whether there's some inversion T of source such that it's a subtree of target. That is, whether there's a node in target that is identically same in values and structure as T including all of its descendants.

Constraints

n ≤ 12 where n is the number of nodes in target

Example 1

Input

source = [0, [1, null, [3, null, null]], [2, null, null]]

target = [5, [2, null, null], [0, [2, null, null], [1, [3, null, null], null]]]

Output

True



Solution :



                        Solution in C++ :

bool recur(Tree* source, Tree* target) {
    if (!source && !target) return true;
    if (!source || !target || source->val != target->val) return false;
    return ((recur(source->left, target->left) && recur(source->right, target->right)) ||
            (recur(source->left, target->right) && recur(source->right, target->left)));
}
bool solve(Tree* source, Tree* target) {
    if (!source && !target) return true;
    if (!source || !target) return false;
    if (source->val == target->val) {
        return (recur(source, target) || solve(source, target->left) ||
                solve(source, target->right));
    } else {
        return (solve(source, target->left) || solve(source, target->right));
    }
}
                    

                        Solution in Java :

import java.util.*;

class Solution {
    public boolean solve(Tree source, Tree target) {
        if (target == null)
            return source == target;
        return isInv(source, target) || solve(source, target.left) || solve(source, target.right);
    }

    public boolean isInv(Tree s, Tree t) {
        if (s == null || t == null)
            return s == t;
        if (s.val != t.val)
            return false;
        return isInv(s.left, t.right) && isInv(s.right, t.left)
            || isInv(s.left, t.left) && isInv(s.right, t.right);
    }
}
                    

                        Solution in Python : 
                            
# class Tree:
#     def __init__(self, val, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def solve(self, source, target):
        if not source or not target:
            return not source and not target
        t = self.isSubtree(target, source)
        return t

    def isSubtree(self, s, t):
        def sametree(s, t):
            if not s or not t:
                return not s and not t
            elif s.val == t.val:
                return (sametree(s.left, t.left) and sametree(s.right, t.right)) or (
                    sametree(s.right, t.left) and sametree(s.left, t.right)
                )
            else:
                return False

        if not s:
            return False
        elif sametree(s, t):
            return True
        else:
            return self.isSubtree(s.right, t) or self.isSubtree(s.left, t)
                    

View More Similar Problems

Compare two linked lists

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0. Example: list1=1->2->3->Null list2=1->2->3->4->Null The two lists have equal data attributes for the first 3 nodes. list2 is longer, though, so the lis

View Solution →

Merge two sorted linked lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the heads of two sorted linked lists, merge them into a single, sorted linked list. Either head pointer may be null meaning that the corresponding list is empty. Example headA refers to 1 -> 3 -> 7 -> NULL headB refers to 1 -> 2 -> NULL The new list is 1 -> 1 -> 2 -> 3 -> 7 -> NULL. Function Description C

View Solution →

Get Node Value

This challenge is part of a tutorial track by MyCodeSchool Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node. The tail is at postion 0, its parent is at 1 and so on. Example head refers to 3 -> 2 -> 1 -> 0 -> NULL positionFromTail = 2 Each of the data values matches its distance from the t

View Solution →

Delete duplicate-value nodes from a sorted linked list

This challenge is part of a tutorial track by MyCodeSchool You are given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete nodes and return a sorted list with each distinct value in the original list. The given head pointer may be null indicating that the list is empty. Example head refers to the first node in the list 1 -> 2 -

View Solution →

Cycle Detection

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0. Example head refers 1 -> 2 -> 3 -> NUL The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0. head refer

View Solution →

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →