g0901_1000.s0951_flip_equivalent_binary_trees.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
package g0901_1000.s0951_flip_equivalent_binary_trees;
// #Medium #Depth_First_Search #Tree #Binary_Tree
// #2022_12_26_Time_0_ms_(100.00%)_Space_40.2_MB_(73.95%)
import com_github_leetcode.TreeNode;
/*
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public boolean flipEquiv(TreeNode root1, TreeNode root2) {
if (root1 == null && root2 == null) {
return true;
}
if (root1 == null || root2 == null) {
return false;
}
if (root1.val != root2.val) {
return false;
}
return flipEquiv(root1.left, root2.left) && flipEquiv(root1.right, root2.right)
|| flipEquiv(root1.left, root2.right) && flipEquiv(root1.right, root2.left);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy