Premium Only Content
![951. Flip Equivalent Binary Trees](https://1a-1791.com/video/s8/6/M/h/s/N/MhsNu.qR4e.1.jpg)
951. Flip Equivalent Binary Trees
Code:-
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool flipEquiv(TreeNode* root1, TreeNode* root2) {
return bfs(root1, root2);
}
private:
bool bfs(TreeNode* root1, TreeNode* root2) {
if (root1 == nullptr && root2 == nullptr) {
return true;
}
if (root1 == nullptr || root2 == nullptr) {
return false;
}
if (root1->val != root2->val) {
return false;
}
return (bfs(root1->left, root2->left) && bfs(root1->right, root2->right)) ||
(bfs(root1->left, root2->right) && bfs(root1->right, root2->left));
}
};
Question:-
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.
A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations.
Given the roots of two binary trees root1 and root2, return true if the two trees are flip equivalent or false otherwise.
Example 1:
Flipped Trees Diagram
Input: root1 = [1,2,3,4,5,6,null,null,null,7,8], root2 = [1,3,2,null,6,4,5,null,null,null,null,8,7]
Output: true
Explanation: We flipped at nodes with values 1, 3, and 5.
Example 2:
Input: root1 = [], root2 = []
Output: true
Example 3:
Input: root1 = [], root2 = [1]
Output: false
Constraints:
The number of nodes in each tree is in the range [0, 100].
Each tree will have unique node values in the range [0, 99].
-
26:56
MYLUNCHBREAK CHANNEL PAGE
13 hours agoUnder The Necropolis - Pt 6
69.8K29 -
UPCOMING
DLDAfterDark
4 minutes agoDLD Live! Today's Leftist Extremists - Feat. Fattac Josh & Red Dawn Readiness
-
LIVE
Break The Cycle w/ Joshua Smith
23 hours agoBreak The Cycle Ep. 246: The Return w/ Vinny Marshall
101 watching -
LIVE
Man in America
6 hours agoThe '3 White Killers' Making Americans Fat, Sick & DEAD w/ Food Chemist Stephen Talcott
954 watching -
LIVE
Tundra Tactical
2 hours agoThe Pew Pew Jew On Tundra Nation Live : The Worlds Okayest Gun Live Stream
351 watching -
7:36
Colion Noir
5 hours agoDonald Trump Issues Executive Order To Protect The Second Amendment
32.8K37 -
13:39
Exploring With Nug
11 hours ago $0.72 earnedCars Found Underwater While Searching Georgia Woman!
39.6K1 -
56:50
IsaacButterfield
1 day ago $1.45 earnedSam Kerr Goes To Jail | Americas Worst Law | Teacher Of The Year
46.3K16 -
6:14
Silver Dragons
1 day agoAmerican Silver Eagle Coins - Dealer Reveals Everything You NEED to Know
59.1K7 -
19:18
Neil McCoy-Ward
1 day ago🚨 The USAID Scandal Goes Way Deeper Than We Could Have Imagined!
57.6K30