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

For example, this binary tree is symmetric:

    1
/ \
2 2
/ \ / \
3 4 4 3

But the following is not:

    1
/ \
2 2
\ \
3 3

Note:
Bonus points if you could solve it both recursively and iteratively.

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

判断一棵树是不是对称的,那么我们需要对比两个位置对称的节点,首先判断这两个节点的值是否相等,然后判断这两个节点的子树是否对称。这就是递归的思路,从根节点的左右子树开始,递归向下。代码如下:

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

LeetCode OJ 101. Symmetric Tree的更多相关文章

  1. <LeetCode OJ> 101. Symmetric Tree

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

  2. [LeetCode&Python] Problem 101. Symmetric Tree

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

  3. 【leetcode❤python】101. Symmetric Tree

    #-*- coding: UTF-8 -*-# Definition for a binary tree node.# class TreeNode(object):#     def __init_ ...

  4. Leetcode 笔记 101 - Symmetric Tree

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

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

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

  6. Leetcode之101. Symmetric Tree Easy

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

  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 (2 solutions)

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

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

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

  10. LeetCode(1) Symmetric Tree

    从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, sym ...

随机推荐

  1. 通过jvisualvm查看JVM的状态

    安装了JDK之后,可以通过jvisualvm 命令进行性能分析

  2. L3-002. 堆栈

    L3-002. 堆栈 题目链接:https://www.patest.cn/contests/gplt/L3-002 线段树 线段树的数据修改和查询都是O(lgn)的,此题只需维护各个区间内的数的个数 ...

  3. Hierarchyid(层次结构)数据类型

    实例表结构 CREATE TABLE [dbo].[Emp]( ,), ), [Org] [hierarchyid], ) INSERT INTO Emp(Name,Org) VALUES('吴xx' ...

  4. 【定位:PDF文件定位关键字所在坐标和页码】

    iText简介: iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件 ...

  5. 关于css3中transform的理解(只是改变状态未改变其真正的属性)

    众所周知,在css3中可以用animation实现动画效果,在这里用一个transform:translateX举例. <div class="div1"></d ...

  6. [妙味JS基础]第十一课:字符串、查找高亮显示

    知识点总结 字符串方法 var str = '2014年新春快乐哈' * length 字符串长度 str.length =>10 ------------------------------- ...

  7. 《JS权威指南学习总结--8.8 函数式编程和8.8.1使用函数处理数组》

    内容要点:    和Lisp.Haskell不同,JS并非函数式编程语言,但在JS中可以像操控对象一样操控函数,   也就是说可以在JS中应用函数式编程技术.ES5中的数组方法(诸如map()和red ...

  8. java web应用程序目录

    WEB-INF是用来存储服务端配置文件信息和在服务端运行的类文件的,它下面的东西不允许客户端直接访问的.

  9. javascript基础(五)函数

    原文http://pij.robinqu.me/ 通过call和apply间接调用函数(改变this) call 和 apply带有多个参数,call和apply把当前函数的this指向第一个参数给定 ...

  10. POJ 2485 Highway(Prim+邻接矩阵)

    ( ̄▽ ̄)" //求最短总路径中的最大边长,Prim还需要一个Max变量 #include<iostream> #include<cstdio> #include&l ...