101. Symmetric Tree

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.

package leetcode.easy;

/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class SymmetricTree {
public boolean isSymmetric1(TreeNode root) {
return isMirror(root, root);
} public boolean isMirror(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) {
return true;
}
if (t1 == null || t2 == null) {
return false;
}
return (t1.val == t2.val) && isMirror(t1.right, t2.left) && isMirror(t1.left, t2.right);
} public boolean isSymmetric2(TreeNode root) {
java.util.Queue<TreeNode> q = new java.util.LinkedList<>();
q.add(root);
q.add(root);
while (!q.isEmpty()) {
TreeNode t1 = q.poll();
TreeNode t2 = q.poll();
if (t1 == null && t2 == null) {
continue;
}
if (t1 == null || t2 == null) {
return false;
}
if (t1.val != t2.val) {
return false;
}
q.add(t1.left);
q.add(t2.right);
q.add(t1.right);
q.add(t2.left);
}
return true;
} @org.junit.Test
public void test1() {
TreeNode tn11 = new TreeNode(1);
TreeNode tn21 = new TreeNode(2);
TreeNode tn22 = new TreeNode(2);
TreeNode tn31 = new TreeNode(3);
TreeNode tn32 = new TreeNode(4);
TreeNode tn33 = new TreeNode(4);
TreeNode tn34 = new TreeNode(3);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = tn31;
tn21.right = tn32;
tn22.left = tn33;
tn22.right = tn34;
tn31.left = null;
tn31.right = null;
tn32.left = null;
tn32.right = null;
tn33.left = null;
tn33.right = null;
tn34.left = null;
tn34.right = null;
System.out.println(isSymmetric1(tn11));
System.out.println(isSymmetric2(tn11));
} @org.junit.Test
public void test2() {
TreeNode tn11 = new TreeNode(1);
TreeNode tn21 = new TreeNode(2);
TreeNode tn22 = new TreeNode(2);
TreeNode tn32 = new TreeNode(4);
TreeNode tn34 = new TreeNode(3);
tn11.left = tn21;
tn11.right = tn22;
tn21.left = null;
tn21.right = tn32;
tn22.left = null;
tn22.right = tn34;
tn32.left = null;
tn32.right = null;
tn34.left = null;
tn34.right = null;
System.out.println(isSymmetric1(tn11));
System.out.println(isSymmetric2(tn11));
}
}

LeetCode_101. Symmetric Tree的更多相关文章

  1. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  2. 【leetcode】Symmetric Tree

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  3. 38. Same Tree && Symmetric Tree

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

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

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

  5. LeetCode之“树”:Symmetric Tree && Same Tree

    Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...

  6. LeetCode: Symmetric Tree 解题报告

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

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

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

  8. 【LeetCode】101. Symmetric Tree (2 solutions)

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  9. &lt;LeetCode OJ&gt; 101. Symmetric Tree

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

随机推荐

  1. Spring如何给静态变量注入值

    Common.java是一个工具类. Spring无法直接给静态变量注入值,因为静态变量不属于对象,只属于类,也就是说在类被加载字节码的时候变量已经初始化了,也就是给该变量分配内存了,导致spring ...

  2. flask 框架 转载:https://cloud.tencent.com/developer/article/1465949

    1.cookie.py """ - 解释: 用来保持服务器和浏览器交互的状态的, 由服务器设置,存储在浏览器 - 作用: 用来做广告推送 - cookie的设置和获取 - ...

  3. 洛谷P1972 HH的项链【树状数组】

    题目:https://www.luogu.org/problemnew/show/P1972 题意:给定一个长度为n的序列,数字表示珠子的种类.m次查询每次询问给定区间内珠子的种类数. 思路:可以说是 ...

  4. 使用jQuery快速高效制作网页交互特效---JavaScript对象及初始面向对象

    一.JavaScript中的基本数据类型 number(数值类型)    string(字符串类型)    boolean(布尔类型)    null(空类型)    undefined(未定义类型) ...

  5. 【编程语言】Kotlin之扩展函数

    简介: 平时Android开发中会使用各种各样的工具类函数,大部分工具类都是对原有对象的一种扩展,例如: public static void startActivity(Activity act, ...

  6. 学到了林海峰,武沛齐讲的Day17完-6 文件操作

    参考   https://www.cnblogs.com/linhaifeng/articles/5984922.html f=open('陈粒1',encoding='utf-8')     ope ...

  7. GitLab获取人员参与项目-贡献项目列表

    目录 前言 获取token 登录 获取用户参与项目 完整代码 前言 最近在做的统计报表项目包含人员代码提交量. 要获取人员代码提交量首先要知道人员参与的项目.GitLab个人页面中有Contribut ...

  8. 简述with原理

    with open('x') as f: for line in f: print(line.replace('a', 'b')) 不管在处理文件过程中是否发生异常,都能保证 with 语句执行完毕后 ...

  9. Java学习日记基础篇(七) —— 数组、排序

    数组 为什么要有数组? 案例:一个养鸡场有六只鸡,他们的体重分别为3kg,5kg,1kg,3.4kg,2kg,50kg.请问这六只鸡的总体重和平均体重是多少? public class test5 { ...

  10. IDEA Junit FileNotFoundException: class path resource [spring/spring.xml] cannot be opened because it does not exist

    今天打算写一个单元测试,但是已经有写好的单元测试无论怎么弄都提示文件不存在,自己一度以为是启动方式不正确.这里简单记录一下处理过程 1 异常信息: Caused by: org.springframe ...