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. BZOJ2956: 模积和——整除分块

    题意 求 $\sum_{i=1}^n \sum_{j=1}^m (n \ mod \ i)*(m \ mod \ j)$($i \neq j$),$n,m \leq 10^9$答案对 $1994041 ...

  2. CSP模拟赛 Lost My Music(二分,可回退化栈)

    题面 题解 发现是斜率的形式,答案的相反数可以看做一条直线的斜率.那么我们要答案最小,斜率最大.维护下凸壳就行了. 考试时写了直接dfsdfsdfs+暴力弹栈拿了808080分(还以为自己是O(n)正 ...

  3. msf爆破

    SSH服务口令猜解: msf > use auxiliary/scanner/ssh/ssh_loginmsf auxiliary(ssh_login) > show optionsmsf ...

  4. 2019 ICPC Asia Xuzhou Regional

    目录 Contest Info Solutions A. Cat B. Cats line up C. <3 numbers E. Multiply F. The Answer to the U ...

  5. A^B Mod C (快速幂)

    题目描述: 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. Input3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= ...

  6. gulp4配置多页面项目编译打包

    又开始公司的新项目了... 那当我们拿到公司新项目的时候我们需要做些什么呢? 下面就来分享一下我的工作步骤吧(仅使用于初学者,大神勿见怪- -,有不好的地方希望指出,十分感谢) 1. 整版浏览 这是一 ...

  7. python常用函数1

    map()函数 map()是python 内置 的高届函数 ,接收一个函数  f  和一个list,并通过把函数  f  依次作用在list的每个元素上,得到一个新的 list 并返回. 比如,对于l ...

  8. 搜索sqlserver 存储过程中的关键字

    搜索sqlserver 存储过程中的关键字 select * from sys.all_sql_modules where definition like '%SP_NAME%'

  9. json页面解析

    List<TjfxDTO> cyjbList = new ArrayList<TjfxDTO>(); cyjbList=tjfxService.cyjb_wcjd(tjfxDT ...

  10. ICEM-圆锥的一种画法

    原视频下载地址:https://yunpan.cn/cqK53dKBnduM9  访问密码 42be ​