【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/symmetric-tree/
Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy
题目描述
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.
题目大意
判断一棵二叉树是不是镜像二叉树。
解题方法
DFS
如果一个树是对称的,那么意味着左右子树是镜像的。就像判断回文一样,每层的 左右两边对称位置 的节点是镜像的。
1
/ \
left-> 2 2 <-right
/ \ / \
3 4 4 3
定义新函数isMirror(left, right),该函数的意义是判断left 和 right是不是对称的子树。
- 当
left和right的值相等的时候,需要判断下一层是否是对称的。 - 在递归判断下一层的时候的时候,需要判断的是
left.left和right.right这两棵树是不是对称的,以及left.right和right.left这两棵树是不是对称的。
代码只是上面的思路的实现,可以用递归来完成。递归最重要的是 要明白函数的定义、输入、输出,如果这些没明白一定会把自己绕进去。另外递归的时候应该把递归函数当做黑盒使用,即不需要知道此函数内部怎么实现的,但是调用这个递归函数就是能达到某个功能。这样会帮助理解递归。
本题提醒了我们:在递归的过程中不一定只有一个参数,也可以同时传了两个参数,每次递归的时候同时改变两个采纳数。
Java代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
return isMirror(root,root);
}
public boolean isMirror(TreeNode left,TreeNode right){
if(left == null && right == null) return true;
if(left == null || right == null) return false;
return (left.val == right.val) && isMirror(left.left,right.right) && isMirror(left.right,right.left);
}
}
AC:1ms
二刷的时候写的Python解法如下:
# 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):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root: return True
return self.isMirror(root.left, root.right)
def isMirror(self, left, right):
if not left and not right: return True
if not left or not right: return False
return left.val == right.val and self.isMirror(left.left, right.right) and self.isMirror(left.right, right.left)
BFS
BFS 使用一个队列,把要判断是否为镜像的节点放在一起。
在队列中同时取出两个节点left, right,判断这两个节点的值是否相等,然后把他们的孩子中按照(left.left, right.right) 一组,(left.right, right.left)一组放入队列中。
BFS做法需要把所有的节点都检查完才能确定返回结果True,除非提前遇到不同的节点值而终止返回False。
Java代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSymmetric(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
q.add(root);
while(!q.isEmpty()){
TreeNode left=q.poll();
TreeNode right=q.poll();
if(left==null && right==null) continue;
if(left==null || right==null) return false;
if(left.val != right.val) return false;
q.add(left.left);
q.add(right.right);
q.add(left.right);
q.add(right.left);
}
return true;
}
}
AC:3ms
二刷的时候的Python解法如下:
# 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):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if not root: return True
que = collections.deque()
que.append(root.left)
que.append(root.right)
while que:
left, right = que.popleft(), que.popleft()
if not left and not right:
continue
if not left or not right:
return False
if left.val != right.val:
return False
que.append(left.left)
que.append(right.right)
que.append(left.right)
que.append(right.left)
return True
一种便于理解的BFS做法,把要判断的是否对称节点作为tuple一起放入队列中:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
queue = collections.deque()
queue.append((root, root))
while queue:
left, right = queue.popleft()
if not left and not right:
continue
if not left or not right:
return False
if left.val != right.val:
return False
queue.append((left.left, right.right))
queue.append((left.right, right.left))
return True
日期
2016 年 05月 8日
2018 年 11 月 19 日 —— 周一又开始了
2020 年 5 月 31 日 —— 准备组织每日一题的分享
【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)的更多相关文章
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- 【LeetCode】Symmetric Tree(对称二叉树)
这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...
- [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 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- (二叉树 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] Symmetric tree 对称二叉树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- Java for 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 ----- java
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- 【GS基础】植物基因组选择研究人员及数量遗传学发展一览
目录 1.GS研究 2.数量遗传发展 GS应用主要在国外大型动物和种企,国内仍以学术为主.近期整理相关学术文献,了解到一些相关研究人员,记录下备忘查询,但不可能全面. 1.GS研究 Theo Meuw ...
- Metabolomics Workfolw
推荐R语言界的国内大佬于淼写的代谢组学workflow,包含了大部分代谢组学(以及暴露组)的数据分析方法. Meta-Workflow 主要内容包括: Sample collection Pretre ...
- window修改dns本地文件
文件地址: C:\Windows\System32\drivers\etc 先修改权限: 最后用记事本打开编辑保存即可
- Excel-转换单元格格式的函数或“方法”汇总
14.转换单元格格式的函数或"方法"汇总 =value(单元格) #转换为数值 =A1&"" #转换A1为文本 = ...
- excel-大于0的数值标记红色且标记红色上箭头,小于0的数值标记绿色且标记绿色下箭头,等于0的数值标记黄色且标记右箭头
0.数值是常规的数值: [蓝色]"↑"0;[红色]"↓"0;[黄色]"→"0 [蓝色]"↑"0.0;[红色]" ...
- linux中chage命令的基本使用
在Linux中chage命令常用于设置系统用户的账户属性 Usage: chage [options] LOGIN Options: -d, --lastday LAST_DAY set date o ...
- 20-Integer to Roman-Leetcode
比较简单的思路:用map存放各个位的数字到罗马字符的映射 然后从个位依次遍历高位加上映射即可. Given an integer, convert it to a roman numeral. Inp ...
- 使用Mybatis出现的问题+配置优化+ResultMap
一.可能出现的问题 1.Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: ...
- C#数据库连接方式【简版】
using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using ...
- Yarn 容量调度器多队列提交案例
目录 Yarn 容量调度器多队列提交案例 需求 配置多队列的容量调度器 1 修改如下配置 SecureCRT的上传和下载 2 上传到集群并分发 3 重启Yarn或yarn rmadmin -refre ...