前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 951 Flip Equivalent Binary Trees

LeetCode 951 Flip Equivalent Binary Trees

作者头像
Yano_nankai
发布2019-02-25 16:26:41
5930
发布2019-02-25 16:26:41
举报
文章被收录于专栏:二进制文集二进制文集

题目描述

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.

Write a function that determines whether two binary trees are flip equivalent. The trees are given by root nodes root1 and root2.

Example 1:

代码语言:javascript
复制
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.
Flipped Trees Diagram

Note:

代码语言:javascript
复制
Each tree will have at most 100 nodes.
Each value in each tree will be a unique integer in the range [0, 99].

代码

代码语言:javascript
复制
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));
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.01.13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档