题目描述:

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

解题思路:

递归法

代码如下:

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

  

Java [Leetcode 101]Symmetric Tree的更多相关文章

  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. leetcode 101 Symmetric Tree ----- java

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

  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. 剑指offer--面试题16

    #include<stack> //思路:遍历链表过程中,将各个指针入栈,再出栈进行反转 ListNode* ReverseList(ListNode* pHead) { if(pHead ...

  2. Java多线程——<一>概述、定义任务

    一.概述 为什么使用线程?从c开始,任何一门高级语言的默认执行顺序是“按照编写的代码的顺序执行”,日常开发过程中写的业务逻辑,但凡不涉及并发的,都是让一个任务顺序执行以确保得到想要的结果.但是,当你的 ...

  3. spoj 274

    离散化   枚举行  扫描横坐标 #include <iostream> #include <cstdio> #include <cstring> #include ...

  4. spoj 138

    离散化  去掉重复点 排序  二分查找 #include<cstdio> #include<cstring> #include<algorithm> #define ...

  5. hdu 1333 Smith Numbers

    刚开始没看清题意,要找的数一定要是素数 ;}

  6. http://www.oschina.net/translate/elasticsearch-getting-started?cmp

    http://www.oschina.net/translate/elasticsearch-getting-started?cmp

  7. Eclipse 修改项目名称

    1:修改项目目录下:.project文件 <?xml version="1.0" encoding="UTF-8"?>               ...

  8. 修改MDI工程主框架窗口标题(修改CREATESTRUCT结构体)

    版权声明:本文为博主原创文章,未经博主允许不得转载. //在CMainFrame类的PreCreateWindow函数中加入 m_strTitle = _T("Hello"); c ...

  9. 谷歌上不了?hoststool一键搞定host 支持在线更新

    https://hosts.huhamhire.com/ http://serve.netsh.org/pub/ipv4-hosts/

  10. Windows下Java File对象创建文件夹时的一个"坑"

    import java.io.File; import java.io.IOException; public class DirCreate { public static void main(St ...