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. ACE Socket Wrapper Facade

    ACE Socket Wrapper Facade 1:Socket API 1.1 IPC(进程间通信) IPC分为本地IPC(同一计算机上的不同进程)和远程IPC(网络互联的不同计算机),本地IP ...

  2. input 和raw_input

    ---恢复内容开始--- 因为看python2 和 3 混了,所以两者里面的东西有点乱input 和raw_input 今天终于搞明白了,在这里记录一下: 1.python 2 中raw_input ...

  3. 实验吧Web-PHP大法

    笔记 字符串比对解析,与大小写无关. eregi()函数 语法: eregi(string pattern, string string, array [regs]); 返回值: 整数/数组 特点:P ...

  4. 5-jQuery - AJAX get()/post()页面请求即执行

    get 前台页面 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> &l ...

  5. Java中SJBArrayList自己简单实现ArrayList

    /** * 自己实现ArrayList * @author zyyt * */ public class SJBArrayList { //存放SJBArrayList中的元素 transient O ...

  6. java default使用

    我们都知道在Java语言的接口中只能定义方法名,而不能包含方法的具体实现代码.接口中定义的方法必须在接口的非抽象子类中实现.下面就是关于接口的一个例子: public interface Simple ...

  7. MongoDB数据模型(二)

    原文地址 接上一篇 四.模型树结构 父引用的模型树结构 这个数据模型描述了一个树形结构,在子节点中存储父节点的引用. 模式 父引用模式存储每个树节点到文档中,除了树节点外,文档还存储了父节点的id. ...

  8. 浅析const标识符在C++函数的功能

    范例: class matrix { public: matrix(){}; const double getvalue(const unsigned row, const unsigned colu ...

  9. linux 移除svn文件夹

    find . -name .svn -type d -exec rm -fr {} \;

  10. ESFramework 通信框架安全机制的设计与实现

    在分布式通信系统中,安全无疑是非常重要的.ESFramework通信框架提供了哪些安全保障了?由于ESFramework通信框架是应用层的开发框架,那么本文我们只讨论ESFramework通信框架在应 ...