【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 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
androot2
.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.
![]()
Note:
- Each tree will have at most
100
nodes.- Each value in each tree will be a unique integer in the range
[0, 99]
.
解题思路:从两个根节点开始分别同步遍历两棵树,如果左右子树相同则表示不用交换;如果左右互相等于对方则交换;否则表示无法交换。
代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
res = True
def verify(self,node1,node2):
leftV1 = None if node1.left == None else node1.left.val
leftV2 = None if node2.left == None else node2.left.val
rightV1 = None if node1.right == None else node1.right.val
rightV2 = None if node2.right == None else node2.right.val
if leftV1 == leftV2 and rightV1 == rightV2:
return 0
elif leftV1 == rightV2 and rightV1 == leftV2:
return 1
else:
return -1
def traverse(self,node1,node2):
if node1 == None or node2 == None:
return
ret = self.verify(node1,node2)
if ret == 0:
self.traverse(node1.left,node2.left)
self.traverse(node1.right, node2.right)
elif ret == 1:
node2.left,node2.right = node2.right,node2.left
self.traverse(node1.left, node2.left)
self.traverse(node1.right, node2.right)
else:
self.res = False def flipEquiv(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: bool
"""
if (root1 == None) ^ (root2 == None):
return False
self.res = True
self.traverse(root1,root2)
return self.res
【leetcode】951. Flip Equivalent Binary Trees的更多相关文章
- 【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- #Leetcode# 951. Flip Equivalent Binary Trees
https://leetcode.com/problems/flip-equivalent-binary-trees/ For a binary tree T, we can define a fli ...
- 113th LeetCode Weekly Contest Flip Equivalent Binary Trees
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left a ...
- 【LeetCode】617. Merge Two Binary Trees 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【leetcode】617. Merge Two Binary Trees
原题 Given two binary trees and imagine that when you put one of them to cover the other, some nodes o ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [Swift]LeetCode951. 翻转等价二叉树 | Flip Equivalent Binary Trees
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left a ...
- 【Leetcode_easy】617. Merge Two Binary Trees
problem 617. Merge Two Binary Trees 参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完
- 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前序遍历 日期 题目地址:https://leetc ...
随机推荐
- 硬盘监控和分析工具:Smartctl
https://linux.cn/article-4682-1.html Smartctl(S.M.A.R.T 自监控,分析和报告技术)是类Unix系统下实施SMART任务命令行套件或工具,它用于打印 ...
- 【Flutter学习】之绘画实例(一)
一,概述 画布(Canvas) 画布是一个矩形区域,我们可以控制其每一像素来绘制我们想要的内容 Canvas 拥有多种绘制点.线.路径.矩形.圆形.以及添加图像等的方法,结合这些方法我们可以绘制出千变 ...
- 微信jssdk安卓机分享QQ好友和QQ空间出现{"errMsg":"shareQQ:fail"}
使用ajax请求appid之类的配置,然后进行wx.config和wx.ready,苹果机上是完全OK的,但是安卓机上十次有九次是失败,只有一次能成功,百度了一下,有人说是参数有空格,有人说是微信bu ...
- Android学习--apk打包过程
1. 使用aapt工具,给所有的res目录下的资源文件生成对应的id,id会被放进R.java文件中 2. JavaC编译器,将所有Java文件转换为Class文件,其中,内部类会分别生成.class ...
- react教程 — 开发 总结
本文章是在熟练使用 VUE 的基础上,对比VUE 功能进行的一个技术总结. 1.react项目快速搭建 https://blog.csdn.net/mapbar_front/article/deta ...
- Redis入门很简单之三【常见参数配置】
Redis入门很简单之三[常见参数配置] 博客分类: NoSQL/Redis/MongoDB redisnosql缓存中间件memcached Redis的一下常见设置都是通过对redis.conf ...
- HTML-参考手册: 元素和有效 DOCTYPES
ylbtech-HTML-参考手册: 元素和有效 DOCTYPES 1.返回顶部 1. HTML 元素和有效 DOCTYPES HTML 元素 - 有效 DOCTYPES 下面的表格列出了所有的 HT ...
- javaScript 通过location对象获取项目的url
项目中有些要通过jQuery 动态加载,其中需要一些路径,使用相对路径会出现错误,报 $("#t1").html("设置或返回从井号 (#) 开始的 URL(锚)---& ...
- upc组队赛7 Slimming Plan
Slimming Plan 题目描述 Chokudai loves eating so much. However, his doctor Akensho told him that he was o ...
- 基于MFC的Media Player播放器的制作(4---功能实现代码)
| 版权声明:本文为博主原创文章,未经博主允许不得转载. PandaPlayerDlg.h // PandaPlayerDlg.h : header file // //{{AFX_INCLUDE ...