给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称)。
例如,这个二叉树 [1,2,2,3,4,4,3] 是对称的。
    1
   / \
  2   2
 / \ / \
3  4 4  3
但是下面这个 [1,2,2,null,3,null,3] 则不是:
    1
   / \
  2   2
   \   \
   3    3
说明:
如果你可以递归地和迭代地解决它就奖励你点数。
详见:https://leetcode.com/problems/symmetric-tree/description/

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 isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return helper(root.left,root.right);
}
private boolean helper(TreeNode left,TreeNode right){
if(left==null&&right==null){
return true;
}else if(left==null||right==null){
return false;
}else if(left.val!=right.val){
return false;
}else{
return helper(left.left,right.right)&&helper(left.right,right.left);
}
}
}

101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树的更多相关文章

  1. LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

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

  2. LeetCode 101. Symmetric Tree 判断对称树 C++

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

  3. 101. Symmetric Tree -- 判断树结构是否对称

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

  4. <LeetCode OJ> 101. Symmetric Tree

    101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...

  5. Leetcode之101. Symmetric Tree Easy

    Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...

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

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

  7. leetcode 100. Same Tree、101. Symmetric Tree

    100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...

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

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

  9. 【一天一道LeetCode】#101. Symmetric Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. 整合kxmovie

    下载后发现kxmovie 直接运行是不通的,上面我们已经处理了ffmpeg部分的内容.当然了先处理ffmpeg部分.然后再复制kxmovie/kxmovie到项目中导入相应的文件. 编译报错:UIim ...

  2. Gradle build-info.xml not found for module app.Please make sure that you are using gradle plugin '2.0.0-alpha4' or higher.

    解决方法:去掉“Enable Instant run to host swap code/resource changes on deploy(default enabled)”的勾选项 Settin ...

  3. 用php描述二分查找法

    //二分查找 $arr = array(0,1,2,3,4,5,6,7,8,9); function bin_sch($array, $low, $high, $k){ if ($low <= ...

  4. EDM文件编写规范及注意事项

    [设计EDM邮件] (1)乱码:你没法知道所有用户的系统环境,因此使用utf8来避免乱码是非常重要的 (2)绝对URL:若是相对URL,用户在打开页面是将看不到图片 (3)图片Alt属性:大多数邮件服 ...

  5. MongoDB复制集成员及架构介绍(一)

    MongoDB复制集介绍 MongoDB支持在多个机器中通过异步复制达到提供了冗余,增加了数据的可用性.MongoDB有两种类型的复制,第一种是同于MySQL的主从复制模式(MongoDB已不再推荐此 ...

  6. 创建calico网络报错client response is invalid json

    使用docker创建calico网络失败. # docker network create --driver calico --ipam-driver calico-ipam testcalico E ...

  7. python读文件和写文件

    f=open('D:\\wangdongjie\\files\\webservice\\baidu\\3.txt','r+') f.write('中国电视台1][][23qwe12f我是一个小小的石头 ...

  8. C#控件刷新

    ; ; foreach (string gen in fn_gen) { //MessageBox.Show(gen); Bitmap Bi = new Bitmap(gen); //使用打开的图片路 ...

  9. 系统管理常用Linux命令

    1.查看系统运行时间.用户数.负载 uptime 11:00:39 up 3 days,  1:08,  1 user,  load average: 0.00, 0.01, 0.05 解析:后面的三 ...

  10. CF-807A

    A. Is it rated? time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...