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

中文:给定一二叉树,检查它是否是它自己的镜像(比如,以中心对称)。

递归:

	public boolean isSymmetric(TreeNode root) {
if(root == null)
return true;
return isSymmetric(root,root);
}
public boolean isSymmetric(TreeNode left,TreeNode right) {
if(left == null && right == null)
return true;
else if(left == null || right == null)
return false;
return left.val == right.val && isSymmetric(left.left,right.right) && isSymmetric(left.right,right.left);
}

LeetCode——Symmetric Tree的更多相关文章

  1. LeetCode: Symmetric Tree 解题报告

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

  2. [LeetCode] Symmetric Tree 判断对称树

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

  3. [leetcode]Symmetric Tree @ Python

    原题地址:https://oj.leetcode.com/problems/symmetric-tree/ 题意:判断二叉树是否为对称的. Given a binary tree, check whe ...

  4. 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现

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

  5. [Leetcode] Symmetric tree 对称二叉树

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

  6. Python3解leetcode Symmetric Tree

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

  7. LeetCode() Symmetric Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  8. leetcode:Symmetric Tree【Python版】

    #error caused by:#1:{} 没有考虑None输入#2:{1,2,2} 没有控制h和t#3:{4,-57,-57,#,67,67,#,#,-97,-97} 没有考虑负号,将s从str变 ...

  9. 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...

随机推荐

  1. php缓存方案

    一.说说Memcached优化方案 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据 ...

  2. Javascript基础引用类型之Object

    虽然说ECMAScript也是一门对象语言,但是它和其他面向对象语言还是有区别的,它不具有类和接口等基本结构.所以在ECMAScript中一般说类指的是引用类型.创建Object实例的方式有两种: 第 ...

  3. OC基础 可变数组与不可变数组的使用

    OC基础 可变数组与不可变数组的使用 1.不可变数组 1.1不可变数组的创建 //实例方法 NSArray *array = [[NSArray alloc] initWithObjects:&quo ...

  4. iPhoneKeyboard

    iPhoneKeyboard.Open static function Open (text : string, keyboardType : iPhoneKeyboardType = iPhoneK ...

  5. CSS凹型导航按钮

    一般需求,圆角看起来更加舒服,但是下面直角略显生硬 于是设计师有了下面的需求,下面加上小凹型: 凹型?凹型?凹型?有点变态,这怎么实现........... 图片肯定是最先考虑到的,CSS实现有貌似有 ...

  6. Iterable 超级接口

    这是一个老祖宗,一代一代往下拨 collection 的方法如下,是一个跟接口方法如下,见API collection  : add():添加一个元素 addAll():添加一组元素 clear(); ...

  7. rr

        times = gcd(rotdist,length);  printf( ;i<times;i++) {  t = vec[i];   j = i;    ) {   k = j+ r ...

  8. 对话框(alert,prompt,confirm,showModalDialog)

    alert大部分浏览器中会产生阻塞,confirm,prompt都会产生阻塞 关于showModalDialog的介绍:(转自JS中showModalDialog 详细使用) 基本介绍:        ...

  9. symfony2 表单

    正常情况下我还是更喜欢直接写表单而不是使用symfony内置的表单对象来穿件表单,但当我一个表单比较重要,需要csrf保护的时候 我会选择使用这种方法来表单. 官方文档有介绍一大堆通过类创建表单这里我 ...

  10. python文件_改名2

    #手动选择路径,批量改名 import os,re,time,tkFileDialog global i #文件名后面增加后缀:txt,png,bng,jpeg,jpg,gif,zip类型的文件 de ...