首页 > 其他分享 >[LeetCode] 951. Flip Equivalent Binary Trees

[LeetCode] 951. Flip Equivalent Binary Trees

时间:2024-10-24 12:45:04浏览次数:1  
标签:Binary right TreeNode val 951 Equivalent null root1 root2

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:
Example 1
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].

翻转等价二叉树。

我们可以为二叉树 T 定义一个 翻转操作 ,如下所示:选择任意节点,然后交换它的左子树和右子树。

只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转 等价 于二叉树 Y。

这些树由根节点 root1 和 root2 给出。如果两个二叉树是否是翻转 等价 的函数,则返回 true ,否则返回 false 。

思路

题意很直观,判断两棵树是否能通过翻转操作变成一样的。那么大致思路就是从根节点往下看,如果 root 节点一样,就往下看;下一层如果左节点 == 左节点,右节点 == 右节点或者 A 的左节点 == B 的右节点,则说明二者可以通过翻转操作变成一样的。用 DFS 的方法遍历整棵树。

复杂度

时间O(n)
空间O(h) - 树的高度

代码

Java实现

/**
 * 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;
 * }
 * }
 */
class Solution {
    public boolean flipEquiv(TreeNode root1, TreeNode root2) {
        if (root1 == root2) {
            return true;
        }
        if (root1 == null || root2 == null || 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));
    }
}

标签:Binary,right,TreeNode,val,951,Equivalent,null,root1,root2
From: https://www.cnblogs.com/cnoodle/p/18499361

相关文章

  • 闯关leetcode——110. Balanced Binary Tree
    大纲题目地址内容解题代码地址题目地址https://leetcode.com/problems/balanced-binary-tree/description/内容Givenabinarytree,determineifitisheight-balanced.Aheight-balancedbinarytreeisabinarytreeinwhichthedepthofthetwosub......
  • [LeetCode] 1545. Find Kth Bit in Nth Binary String
    Giventwopositiveintegersnandk,thebinarystringSnisformedasfollows:S1="0"Si=Si-1+"1"+reverse(invert(Si-1))fori>1Where+denotestheconcatenationoperation,reverse(x)returnsthereversedstringx,an......
  • 闯关leetcode——94. Binary Tree Inorder Traversal
    大纲题目地址内容解题代码地址题目地址https://leetcode.com/problems/binary-tree-inorder-traversal/description/内容Giventherootofabinarytree,returntheinordertraversalofitsnodes’values.Example1:Input:root=[1,null,2,3]Outpu......
  • BinaryTree
    二叉树树的基本概念树是一种非线性的数据结构n(n>0)个有限节点组成一个具有层次关系的集合像一颗倒挂的树根朝上叶朝下这里重要的是树中的几个概念结点的度:一个结点含有子树的个数比如上面的图A的度为6树的度:一颗树中,所有结点度的最大值成为树的度如上图......
  • [ABC222H] Beautiful Binary Tree 题解
    第一次写拉格朗日反演。思路考虑如何操作。发现出根节点外有\(n-1\)个点是一。由于我们只能操作\(n-1\)次,相当于每一次操作必须把两个一合并。一个点最多往上跳两层,所以要求它的父亲或者爷爷是一。考虑设\(f_i\)表示当前节点为一并且整个子树总和为\(i\)的方案数,\(g......
  • std::binary_function 未定义问题
    使用高版本C++编译器编译旧的SDK的时候,SDK代码中会含有一些已经废弃的函数;如std::binary_function修改方式:原始代码:namespace{structNameCompare:std::binary_function<constchar*,constchar*,bool>{booloperator()(constchar*x,constchar*y)c......
  • postman的post方法中Body项里,none,form-data,x-www-form-urlencoded,raw,binary,Grap
    目录1.None2.form-data3.x-www-form-urlencoded4.raw5.binary6.GraphQL总结在Postman中,使用POST方式时,Body项中有几种不同的数据传输方式可供选择,它们之间的主要区别在于数据的格式和编码方式。以下是每种类型的详细解释:1.None描述:不发送请求体(body)。用途:如果你......
  • 125.785 S1  Binary Model practice
    125.785S12024-- Assignment 2 (Part A and Part B)Duedate:18th October2024Theassignmentis30%tothecourseassessment, including two parts:PartA. Practice binary and paneldata regressions (15%), Part B.Critical Reading (15%).......
  • Solution - Atcoder ARC157E XXYX Binary Tree
    考虑这个不存在\(\texttt{YY}\)的限制,与\(\texttt{XX}\)个数为变量的限制相比较,看起来\(\texttt{Y}\)就更特殊,于是考虑从\(\texttt{Y}\)的视角来分析问题。同时考虑到因为有\(A+B+C=n-1\),所以\(\texttt{XX}\)其实也不是很重要,因为只需要让\(\texttt{XY}\)和......
  • 1043 Is It a Binary Search Tree——PAT甲级
    ABinarySearchTree(BST)isrecursivelydefinedasabinarytreewhichhasthefollowingproperties:Theleftsubtreeofanodecontainsonlynodeswithkeyslessthanthenode'skey.Therightsubtreeofanodecontainsonlynodeswithkeysgreater......