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.

判断一棵树是否是对称的。

/**
* 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;
if( root.left == null && root.right == null)
return true;
if( root.left == null || root.right == null)
return false; return getResult(root.left,root.right); } public boolean getResult(TreeNode left,TreeNode right){
if( left == null && right == null)
return true;
if( left == null || right == null)
return false;
if( left.val != right.val )
return false;
if( getResult(left.left,right.right) )
return getResult(left.right,right.left);
return false; } }

也可以使用队列来解决这个问题。

 

leetcode 101 Symmetric Tree ----- java的更多相关文章

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

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

  2. Leetcode 101 Symmetric Tree 二叉树

    判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...

  3. Java for LeetCode 101 Symmetric Tree

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

  4. Java [Leetcode 101]Symmetric Tree

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

  5. LeetCode 101. Symmetric Tree (对称树)

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

  6. (二叉树 DFS 递归) leetcode 101. Symmetric Tree

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

  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. LeetCode 101. Symmetric Tree 判断对称树 C++

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

  9. Leetcode 101. Symmetric Tree(easy)

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

随机推荐

  1. SeGue 多控制器跨界面传递数据原理

    多控制器跨界面传递数据原理

  2. obj.offsetHeight与obj.style.height区别

    我们都知道obj.offsetHeight与obj.style.height都可以获取obj的高度,但是在js使用中,我们通常会使用前者来获取高度,这是为什么,二者有什么样的区别呢. 1.obj.of ...

  3. Android 监听ContentProvider的数据改变

    今天介绍一下怎么监听ContentProvider的数据改变,主要的方法是:getContext().getContentResolver().notifyChange(uri,null),这行代码是 ...

  4. MySQL语句45道练习题及答案

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  5. 摘自:java夜未眠之java学习之道

    目前Java可以说是产业界和学术界最热门的语言,许多读者都很急切想把Java学好.除非是武侠小说中的运功传送内力的方式,否则花上一段时间苦学是免不了的.花时间,不打紧,就是怕方法错误,事倍功半.我认为 ...

  6. UVa 10318 Security Panel

    题意:给你一个3*3的翻转模版,深色部分表示翻转,浅色部分不变.然后你可以在r*c的矩形里依照模版进行翻转,要求所有点亮所有块.输出最小的步骤. 思路:有一点比较好想.每个块至多被翻转一次,翻两次的效 ...

  7. openstack context

    之前一直不知道context模块中存储的是什么东西,这回看一下代码: 其中最主要的类是:RequestContext: class RequestContext(object):   "&q ...

  8. Java中线程的锁和数据库中的事务隔离级别

    当涉及到两个或多个线程操作同一个资源时,就会出现锁的问题. 数据库中的某一条记录或者是某一个对象中的字段,可以修改,也可以读取,一般情况下,读取的那个方法应该加锁(即用synchronized互斥), ...

  9. MagSpoof:能预测并窃取你下一张信用卡号码的廉价设备

    想象一下,你丢失了信用卡,并从银行申请了一张新的信用卡.但是,如果在你收到这张新卡之前,一些网络罪犯就已经在使用你的新信用卡,此时你作何感想?是的,这完全是可以实现的,至少使用这个仅仅10美元的设备M ...

  10. 深入分析:Fragment与Activity交互的几种方式(一,使用Handler)

    这里我不再详细介绍那写比较常规的方式,例如静态变量,静态方法,持久化,application全局变量,收发广播等等. 首先我们来介绍使用Handler来实现Fragment与Activity 的交互. ...