Leetcode 951. Flip Equivalent Binary Trees (二叉树翻转题)

Lenovo 拯救者15ISK硬件升级实战:从内存到固态硬盘的全面优化 本文详细介绍了为联想拯救者15ISK笔记本电脑进行硬件升级的完整实战指南。通过评估升级可行性,指导如何挑选兼容的内存条与NVMe固态硬盘,并提供详细的拆机安装步骤与系统安装避坑技巧。升级后,笔记本的开机速度、软件加载及多任务处理能力获得飞跃式提升,让老旧设备重获新生。 阅读详情
  1. Flip Equivalent Binary Trees
    Solved
    Medium
    Topics
    Companies
    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].

解法1:

/**
 * 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) {
        if (!root1 && !root2) return true;
        if (!root1 || !root2) return false;
        if (root1->val == root2->val) {
            return (flipEquiv(root1->left, root2->right) && flipEquiv(root1->right, root2->left)) ||
            (flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right));
        } else {
            return false;
        }
    }
};
保姆级教程:用CameraX API快速搞定Android多摄像头切换与预览(附避坑指南) 本文提供了一份详细的CameraX API教程,帮助开发者快速实现Android多摄像头切换与预览功能。通过简化API设计和生命周期感知能力,CameraX大幅降低了开发复杂度,同时解决了多摄像头兼容性、预览优化等常见问题。文章包含完整代码示例和实战避坑指南,特别适合需要高效集成相机功能的Android开发者。 阅读详情

相关推荐

从零构建XVC协议栈:FPGA+W5300的跨界调试架构实战

本文详细介绍了如何从零构建XVC协议栈,通过FPGA与W5300以太网控制器的创新组合实现跨界调试架构。重点解析了JTAG时序逆向、TCP/IP协议栈集成与XVC协议实现,提供了硬件设计优化和性能加速技术,为嵌入式开发者和FPGA工程师提供实用的远程调试解决方案。

raspberrypi5的博客 247

leetcode 951. Flip Equivalent Binary Trees 翻转等价二叉树-耗时100

Problem:耗时100%,可能存在某些node翻转了,对所有nodes只存在两种情况,一种是翻转了,另一种是没有翻转,所以递归的时候,需要满足翻转了完全相同,或者没有翻转完全相同,也就是return (l1 && l2) | (l3 && l4);终止条件就是root1nullptr返回true,其他的返回false。

邹九的个人博客 76

S32DS使用手把手教程:从零实现CAN通信配置

深入讲解S32DS使用过程中如何从零开始配置CAN通信,涵盖关键步骤与实用技巧,帮助开发者快速上手S32DS使用中的常见问题与调试方法。

weixin_42405592的博客 1124

Leetcode 951.翻转等价二叉树(Flip Equivalent Binary Trees)

Leetcode 951.翻转等价二叉树 1 题目描述(Leetcode题目链接)   我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树。 只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉树 Y。 编写一个判断两个二叉树是否是翻转等价的函数。这些树由根节点 root1 和 root2 给出。 输入:root1 = [1,2,3,4,5,6,null,null,null,7,8], root2 = [1,3,2,null,6,4,5

qq_39378221的博客 282

RK3568 MIPI屏幕启动异常:从uboot logo到黑屏的排查与修复

本文详细分析了RK3568平台MIPI屏幕在启动过程中,从uboot阶段正常显示logo到内核阶段黑屏的典型故障。通过硬件测量与软件调试,最终定位问题根源在于电源时序,并提供了在设备树中为屏幕供电的regulator节点添加`regulator-boot-on`属性的关键解决方案,有效解决了因内核启动早期电源管理导致的显示异常。

weixin_29161719的博客 540

PVT(Process Voltage Temperature)

芯片PVT

网始如芯个人博客 3万+

951. Flip Equivalent Binary Trees**

951. Flip Equivalent Binary Trees** https://leetcode.com/problems/flip-equivalent-binary-trees/ 题目描述 For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left ...

珍妮的选择的博客 311

PLC的自抗扰控制(ADRC)算法

1、自抗扰控制算法,网上很多文章有所讲解,大家也可以关注韩京清教授的论文和书籍,这里就不赘述了,下面给出梯形图+SCL代码。 1、跟踪微分器 2、线性控制率 3、博图SCL控制代码 4、ADRC控制算法的所有公式汇总 ......

RXXW_Dor的博客 3852

LeetCode //C - 951. Flip Equivalent Binary Trees

Summary: The problem determines if two binary trees are flip equivalent, meaning one can be transformed into the other via any number of subtree swaps. The solution uses recursion to check at each node whether the subtrees match either in their original or

Made in Code 927

YOLOv11 改进入门篇 | 轻量级涨点技巧:GhostConv 替代普通卷积的实战指南

摘要: 本文介绍如何通过GhostConv改进YOLOv11,实现轻量化涨点。YOLOv11深度增加导致参数量、延迟和内存占用上升,GhostConv通过“本征特征+线性变换”策略,在精度几乎无损的情况下降低30%-50%计算量。文章详细解析GhostConv原理,对比普通卷积的参数量和FLOPs优势,并提供完整的代码级改造流程,包括模块实现、注册及网络配置。实验表明,该方法有效提升边缘端推理效率,适用于移动端等资源受限场景。

shrgegrb的博客 1652

【QNX+Android虚拟化方案】01 - 芯片介绍

【QNX+Android虚拟化方案】01 - 芯片介绍

|~~~热爱生活、努力学习的小伙汁~~~| 2944

Leetcode951. Flip Equivalent Binary Trees翻转等价二叉树

我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树。 只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X翻转等价于二叉树 Y。 编写一个判断两个二叉树是否是翻转等价的函数。这些树由根节点root1和root2给出。 示例: 输入:root1 = [1,2,3,4,5,6,n...

Ha12312的博客 249

EtherCAT ROS2 入门教程(一)

本文介绍了EtherCAT驱动与ROS2的集成方案,主要包括三部分内容:1)在Ubuntu 22.04上安装IgH EtherCAT主站的详细步骤,包括内核配置、源码编译和网络适配器设置;2)构建ethercat_driver_ros2,通过ros2_control描述文件配置硬件接口和EtherCAT从站模块插件;3)使用方法,包括启动EtherCAT主站和运行配置包。该方案支持通过参数文件定义硬件接口,并提供通用和专用两种EtherCAT模块插件,便于开发基于EtherCAT的工业应用。

机器人控制与仿真应用 1528

leetcode 951. 翻转等价二叉树(Flip Equivalent Binary Trees)

目录 题目描述: 示例: 解法: 题目描述: 我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树。 只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉树 Y。 编写一个判断两个二叉树是否是翻转等价的函数。这些树...

weixin_30952103的博客 128

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Leetcode-951 Flip Equivalent Binary Trees

951.Flip Equivalent Binary Trees Medium 32413FavoriteShare 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 tr...

Andreadyx的博客 189

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【LeetCode】951. Flip Equivalent Binary Trees

题目大意:给定两棵树,判断经过一系列旋转操作后是否相等。 刚开始写就想设一个旋转操作,遇到不等的节点就旋转再判断,代码又臭又长。 看了一下讨论区,恍然大悟,原来并不需要真正的旋转,将遍历的顺序改变一下其实就相当于做了旋转:   /** * Definition for a binary tree node. * struct TreeNode { * int val; ...

McGregorWwww的博客 316

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

[leetcode] 951. Flip Equivalent Binary Trees

Description 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

Learning from the mistakes 239

Leetcode学习笔记:#951. Flip Equivalent Binary Trees

Leetcode学习笔记:#951. Flip Equivalent Binary Trees 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 equ...

ccystewart的博客 156

951. Flip Equivalent Binary Trees [JavaScript]

一、题目   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 ...

descire 256

【Leetcode】951. Flip Equivalent Binary Trees

题目地址: https://leetcode.com/problems/flip-equivalent-binary-trees/ 给定两棵二叉树,如果其中一棵树能通过翻转若干次某些节点的左右子树能变成第二棵树,就称它们等价。问它们是否等价。 思路是递归。两棵树等价当且仅当,它们都是空树,或者都不是空树并且树根相等并且左左右右等价,或者都不是空树并且树根相等并且左右右左等价。代码如下: public class Solution { public boolean flipEquiv(TreeNode

数学、算法爱好者的博客 191

leetcode----951. Flip Equivalent Binary Trees

链接: https://leetcode.com/problems/flip-equivalent-binary-trees/ 题意: 定义一种操作 flip:将某一树节点的左子树和右子树互换。给定两棵树,判断两棵树之间是否可以通过flip操作来得到。例子: 思路: (1)若两个都为空,则返回true (2)若一个为空一个不为空,肯定是返回false (3)若根节点的值不一样,肯...

smart_ferry的博客 173

Leetcode 951. Flip Equivalent Binary Trees

/** * 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) {} *

Xurui_Luo的博客 230

【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法递归日期 题目地址:https://leetcode.com/problems/flip-equivalent-binary-trees/description/ 题目描述 For a binary tree T, we can define a flip ...

负雪明烛 2499

LeetCode 951. Flip Equivalent Binary Trees

原题链接在这里:https://leetcode.com/problems/flip-equivalent-binary-trees/ 题目: For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees. ...

weixin_30337157的博客 88

LeetCode 951. Flip Equivalent Binary Trees (Medium)

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...

weixin_45744426的博客 226

LeetCode #951. Flip Equivalent Binary Trees

题目描述: 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 Xisflip equivalentto a binary tree Y if and only i...

LawFile的博客 168
上一篇: LintCode 3693 · Clone N-branch Tree (多叉树构建题)
下一篇: Leetcode 897. Increasing Order Search Tree (二叉树遍历和分治好题)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值