[LeetCode] 101. Symmetric Tree_ Easy tag: BFS
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following [1,2,2,null,3,null,3] is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
这个题目思路就是BFS啦, 只不过把left child 和right child比较了之后再recursively 比较children的children. 我们用一个helper function, 用于比较tree1, 和tree2, 然后recursive call这个function, 然后返回helper(root, root)即可.
1. Constraints
1) root can be None, return True
2. Ideas
BFS, T: O(n) S: O(n) worst case when tree is linear
3. Code
class Solution:
def Symmetric(self, root):
def helper(tree1, tree2):
if not tree1 and not tree2:
return True
if tree1 and tree2 and tree1.val == tree2.val:
return helper(tree1.left, tree2.right) and helper(tree1.right, tree2.left)
return False
return helper(root, root)
4. Test cases
1) root is None
2) root is 1
3)
1
/ \
2 2
/ \ / \
3 4 4 3
[LeetCode] 101. Symmetric Tree_ Easy tag: BFS的更多相关文章
- Leetcode 101. Symmetric Tree(easy)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- [Leetcode] 863. All Nodes Distance K in Binary Tree_ Medium tag: BFS, Amazon
We are given a binary tree (with root node root), a target node, and an integer value `K`. Return a ...
- (二叉树 DFS 递归) leetcode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [LeetCode] 127. Word Ladder _Medium tag: BFS
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- leetcode 101 Symmetric Tree ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- Git学习之Git 暂存区
============================= 修改文件后是否可以直接提交 ============================ (1) 向文件中追加一行内容 $ echo &quo ...
- canvas - drawImage()方法绘制图片不显示的问题
canvas有个很强大的api是drawImage()(w3c): 他的主要功能就是绘制图片.视频,甚至其他画布等. 问题: 慕名赶来,却一脚踩空,低头一看,地上一个大坑. 事情是这样的,在我看完 ...
- enum hack
关于占用内存的大小,enum类型本身是不占内存的,编译器直接替换.但是enum类型的变量肯定是占内存的. class A{ public: //enum类型本身不占内存 enumEnumTest{ a ...
- 让某个软件无法被操作员最小化(C#演示)
有一次在生产线上, 有一个显示激光轮廓的软件被操作员最小化了, 结果悲剧了, 轮廓图像都抓不到了. 原先的设想的操作流程是这个软件是不能被操作员最小化的, 但可惜不能指望员工这么能守规矩. 看来只能在 ...
- 公钥基础设施体系和EJBCA的一些概念
最近一段时间的在公司做的事情是: 1. 为公司的一些线上系统启用https(使用nginx反向代理的方式来实现,之前的应用无需做改动) 2.为符合规则的用户颁发数字证书(自建CA来实现,目前的用途是给 ...
- 【CF739E】Gosha is hunting 贪心
[CF739E]Gosha is hunting 题意:有n个小精灵,你有a个普通球和b个超级球,用普通球抓住第i只小精灵的概率为$A_i$,用超级球抓住第i只小精灵的概率为$u_i$.你必须一开始就 ...
- [sharepoint]Office Web Apps for SharePoint 2010
Office Web Apps for SharePoint 2010 2012年09月20日 ⁄ 综合 ⁄ 共 908字 ⁄ 字号 小 中 大 ⁄ 评论关闭 After you install Of ...
- Unity3D笔记 英保通四 虚拟轴应用及键盘事件
Input: 1.使用这个类能够读取输入管理器设置的按键,以及访问移动设备的多点触控或加速感应数据.想要读取轴向使用Input.GetAxis方法获取下列默认轴: "Horizontal&q ...
- Redis学习资料整理
Redis学习资料: (1)Redis设计与实现 (2)十五分钟介绍 Redis数据结构 (3)redis安装 (4)redis指令手册中文版 Hiredis学习资料: (1)hiredis安装及测试 ...
- url分发、isinstance、request.GET请求之QueryDict和urlencode、post和get请求、limit_choices_to(Model字段)
这个的路径是怎么来的,是有一个个的url路由分发过来的 这两个是相等的,若url后面加括号了,那么前面就不用这个装饰器了:反之,若装饰器使用了,那么这个url后面就不要加括号了 eg:其他的views ...