来源:https://leetcode.com/problems/symmetric-tree

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

递归:

1. 若根节点为null,返回true

2. 若根节点不为null,对其左右子树进行递归地比较:若两个节点都为null,返回true;若一个为null,另一个不为null,返回false;若两个节点(A,B)值相等,则返回 (A.left, B.right) 的比较结果 && (A.right, B.left) 的比较结果。

Java

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean compareLR(TreeNode left, TreeNode right) {
if(left == null && right == null) {
return true;
}
if((left == null && right != null) || (left != null && right == null)) {
return false;
}
if(left.val == right.val) {
return compareLR(left.right, right.left) && compareLR(left.left, right.right);
}
return false;
}
public boolean isSymmetric(TreeNode root) {
if(root == null) {
return true;
}
return compareLR(root.left, root.right);
}
}

Python

 # -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetrical(self, pRoot):
def comRoot(left, right):
if not left:
return not right
if not right or (left.val != right.val):
return False
return comRoot(left.right, right.left) and comRoot(left.left, right.right)
if not pRoot:
return True
return comRoot(pRoot.left, pRoot.right)

迭代:

1. 若根节点为null,返回true

2. 若根节点不为null,将其左右两个子节点入队列

3. 从队列中取出两个节点,若两个节点都为null,返回true;若一个为null,另一个不为null,返回false;若两个节点(A,B)值相等,将他们的两个子节点按照A.left,B.right,A.right,B.left的顺序入队列,重复执行该步,直到队列中不再有节点。

Symmetric Tree(对称二叉树)的更多相关文章

  1. [Leetcode] Symmetric tree 对称二叉树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  2. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  3. 【LeetCode】Symmetric Tree(对称二叉树)

    这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...

  4. [leetcode] 101. Symmetric Tree 对称树

    题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...

  5. 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  6. 第28题:leetcode101:Symmetric Tree对称的二叉树

    给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...

  7. [leetcode]101. Symmetric Tree对称树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  8. Symmetric Tree 对称树

    判断一棵二叉树是否为对称的树.如 1 / \ 2 2 / \ / \ 3 4 4 3 观察上面的树可以看出:左子树的右子树等于右子树的左子树,左子树的左子树等于右子树的右子树. 首先可以使用递归.递归 ...

  9. 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...

  10. 【遍历二叉树】09判断二叉树是否关于自己镜像对称【Symmetric Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,判断是否他自己的镜 ...

随机推荐

  1. 牛客练习赛53E 老瞎眼 pk 小鲜肉(线段树)

    链接:https://ac.nowcoder.com/acm/contest/1114/E来源:牛客网题目:老瞎眼有一个长度为 n 的数组 a,为了为难小鲜肉,他准备了 Q 次询问,每次给出 一个区间 ...

  2. Insomni'hack teaser 2019 - Pwn - 1118daysober

    参考链接 https://ctftime.org/task/7459 Linux内核访问用户空间文件:get_fs()/set_fs()的使用 漏洞的patch信息 https://maltekrau ...

  3. Spring缓存机制(转)

    Spring的缓存机制非常灵活,可以对容器中任意Bean或者Bean的方法进行缓存,因此这种缓存机制可以在JavaEE应用的任何层次上进行缓存. Spring缓存底层也是需要借助其他缓存工具来实现,例 ...

  4. 安装了vs2019 编译node-sass node-gyp 找不到编译器的解决方法

    1 新建powershell脚本文件 <# This is a workaround for "node-gyp is unable to find msbuild if VS2019 ...

  5. js中的 与和或 , && ,||

    || 1.只要"||"前面为false,不管"||"后面是true还是false,都返回"||"后面的值. 2.只要"||&quo ...

  6. 利用BeautifulSoup爬去我爱我家的租房数据

    因为之前对BeautifulSoup一直不是很熟悉,刚好身边的朋友同事在找房子,就想着能不能自己写个爬虫爬一下数据,因此就写了这个爬虫.基本都是边看书边写的,不过也没什么好讲的.直接粘代码了. # c ...

  7. Day_02-Python的循环结构

    循环结构 应用场景 如果在程序中我们需要重复的执行某条或某些指令,例如用程序控制机器人踢足球,如果机器人持球而且还没有进入射门范围,那么我们就要一直发出让机器人向球门方向奔跑的指令.当然你可能已经注意 ...

  8. CF543C Remembering Strings 状压dp

    Code: #include <cstdio> #include <algorithm> #include <cstring> #define setIO(s) f ...

  9. sh_06_个人信息

    sh_06_个人信息 """ 姓名:小明 年龄:18 岁 性别:是男生 身高:1.75 米 体重:75.0 公斤 """ # 在 Pytho ...

  10. Java称霸编程语言排行榜

    笔者精挑细选了本周研发频道的热门看点,供您在这个周末阅读欣赏.内容涵盖TIOBE编程语言8月份排行榜.开源挑战.WebGL演示.HTML5在线工具.IT职业身涯的14个建议,还有即将举行的SDCC(中 ...