来源: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. Docker介绍,安装和常用的命令

    Docker是Google公司推出的Go语言开发的,基于Linux内核的cgroup,namespace,AUFS类的UnionFS等技术.对进程进行封装格力,属于操作系统层面的虚拟化技术.隔离的进程 ...

  2. java String源码浅出

    1.public char charAt(int index) 返回指定索引处的 char 值. 源码: =====================String.class============== ...

  3. 前端框架之BootStrap的简单介绍

    Bootstrap补充 一.一个小知识点 1.截取长屏的操作 2.设置默认格式 3.md,sm, xs 4.空格和没有空格的选择器 二.响应式介绍 - 响应式布局是什么? 同一个网页在不同的终端上呈现 ...

  4. BZOJ1896 Equations 线性规划+半平面交+三分

    题意简述 给你\(3\)个数组\(a_i\),\(b_i\)和\(c_i\),让你维护一个数组\(x_i\),共\(m\)组询问,每次给定两个数\(s\),\(t\),使得 \[ \sum_i a_i ...

  5. js学习之BOM和DOM

    1. 浏览器的原理 1.1 浏览器的多线程 (1)  解析js引擎线程 (2)  UI渲染线程 (3)  事件发起线程 (4)  发起请求的线程 (5)  定时器的线程 1.2 同步异步 (1)  前 ...

  6. java:Conllection中的List,ArrayList添加元素,删除元素,输出元素

    java:Conllection中的List,ArrayList添加元素,删除元素,输出元素 //为list接口实例化 List<String> addlist = new ArrayLi ...

  7. ESP8266-12F

    读者可以把ESP8266当做Arduino+WiFi功能来开发 ESP8266模块支持STA/AP/STA+AP 三种工作模式: STA 模式:ESP8266模块通过路由器连接互联网,手机或电脑通过互 ...

  8. CDMA与OFDM之技术比较

    频谱利用率.支持高速率多媒体服务.系统容量.抗多径信道干扰等因素是目前大多数固定宽带无线接入设备商在选择CDMA(码分多址)或OFDM(正交 频分复用)作为点到多点(PMP)的关键技术时的主要出发点. ...

  9. IDEA将新建项目上传至GitLab

    1.首先,需要你自己登录GitLab,并新建一个项目的链接,如下图所示: (此图为图三,该链接下面操作中将会用到!) 2.在idea上新建一个项目,完成之后,需要创建一个git仓库: 3.然后可以根据 ...

  10. (26)Python获取某个文件存放的相对路径(更改任意目录下保持不变)

    import os import platform def getSeparator(): ''' 获取不同平台下的斜杠符号 :return: Created by Wu Yongcong 2017- ...