
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Inverted Subtree in C++
Suppose we have two binary trees called source and target; we have to check whether there is some inversion T of source such that it is a subtree of the target. So, it means there is a node in target that is identically same in values and structure as T including all of its descendants.
As we know a tree is said to be an inversion of another tree if either −
Both trees are empty
Its left and right children are optionally swapped and its left and right subtrees are inversions.
So, if the input is like source
Target
then the output will be True
To solve this, we will follow these steps −
Define a function check(), this will take node1, node2,
if node1 and node2 both are null, then −
return true
if node1 or node2 any one of them is null, then −
return false
if val of node1 is not equal to val of node2, then −
return false
op1 := check(left of node1, left of node2) and check(right of node1, right of node2)
op2 := check(right of node1, left of node2) and check(left of node1, right of node2)
return true when at least one of op1 and op2 is true
Define a function solve(), this will take source, target,
if source and target are empty, then −
return true
if source or target any one of them is null, then −
return false
op1 := check(target, source)
if op1 is true, then −
return true
return true when at least one of the solve(source, left of target) or solve(source, right of target) is true
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class TreeNode { public: int val; TreeNode *left, *right; TreeNode(int data) { val = data; left = NULL; right = NULL; } }; class Solution { public: bool check(TreeNode* node1, TreeNode* node2){ if(!node1 && !node2) return true; if(!node1 || !node2) return false; if(node1->val != node2->val) { return false; } bool op1 = check(node1->left, node2->left) && check(node1->right, node2->right); bool op2 = check(node1->right, node2->left) && check(node1->left, node2->right); return op1 || op2; } bool solve(TreeNode* source, TreeNode* target) { if(!target && !source) return true; if(!target || !source) return false; bool op1 = check(target, source); if(op1) return true; return solve(source, target->left) || solve(source, target->right); } }; main(){ Solution ob; TreeNode *target = new TreeNode(6); target->left = new TreeNode(3); target->right = new TreeNode(1); target->right->left = new TreeNode(3); target->right->right = new TreeNode(2); target->right->right->left = new TreeNode(4); TreeNode *source = new TreeNode(1); source->left = new TreeNode(2); source->right = new TreeNode(3); source->left->right = new TreeNode(4); cout << (ob.solve(source, target)); }
Input
TreeNode *target = new TreeNode(6); target->left = new TreeNode(3); target->right = new TreeNode(1); target->right->left = new TreeNode(3); target->right->right = new TreeNode(2); target->right->right->left = new TreeNode(4); TreeNode *source = new TreeNode(1); source->left = new TreeNode(2); source->right = new TreeNode(3); source->left->right = new TreeNode(4);
Output
1
- Related Articles
- Largest BST Subtree in C++
- Most Frequent Subtree Sum in C++
- Even size subtree in n-ary tree in C++
- Find largest subtree sum in a tree in C++
- Smallest Subtree with all the Deepest Nodes in C++
- C++ Queries for DFS of a Subtree in a Tree
- Find the largest Complete Subtree in a given Binary Tree in C++
- Maximum path sum in an Inverted triangle in C++
- Maximum Average Subtree in Python\n
- Check if a binary tree is subtree of another binary tree in C++
- Find the largest BST subtree in a given Binary Tree - Set 1 in C++
- Create Inverted Navbar in Bootstrap
- Python Program to Print only Nodes in Left SubTree
- Find the Kth node in the DFS traversal of a given subtree in a Tree in C++
- Querying the number of distinct colors in a subtree of a colored tree using BIT in C++
