101. Symmetric Tree

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.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root == NULL)
{
return true;
}
return isSame(root->left, root->right);
}
bool isSame(TreeNode* left, TreeNode* right)
{
if(left == NULL && right == NULL)
{
return true;
}
else if(left == NULL)
{
return false;
}
else if(right == NULL)
{
return false;
} if(left->val == right->val && left->left == NULL && left->right && right->right == NULL && right->left == NULL)
{
return true;
}
else if(left->val == right->val)
{
return isSame(left->left, right->right) && isSame(left->right, right->left);
}
else
{
return false;
} }
};

leetcode 101的更多相关文章

  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. LeetCode 101. Symmetric Tree (对称树)

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

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

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

  5. Java实现 LeetCode 101 对称二叉树

    101. 对称二叉树 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2 ...

  6. leetcode 101 Symmetric Tree ----- java

    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. Java [Leetcode 101]Symmetric Tree

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

  9. LeetCode 101. Symmetric Tree 判断对称树 C++

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

随机推荐

  1. C++ 局部变量的析构

    http://blog.chinaunix.net/uid-52437-id-2108747.html 在一个函数内,申明一个局部类变量.则这个变量什么时候析构呢? 并不是在函数退出,释放栈空间时候析 ...

  2. ylbtech-dbs:ylbtech-2,PAM(个人资产管理系统)

    ylbtech-dbs:ylbtech-2,PAM(个人资产管理系统) -- =============================================-- Personal Asse ...

  3. oracle常用的数据迁移方法

    源地址:http://wenku.baidu.com/link?url=lI6UYpvDs_y8ku6DytEZLl4GSJjQ0GAGPvv8txrbRoQKgqzTCMAfBZI5mn9t-KQk ...

  4. 微信中得到的GPS经纬度放在百度,腾迅地图中不准的原因及处理

    微信中可以得到两种GPS坐标信息  默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02' 一种是全球的正常GPS坐标信息 wgs84 . GPS,W ...

  5. ElasticSearch 的 聚合(Aggregations)

    Elasticsearch有一个功能叫做 聚合(aggregations) ,它允许你在数据上生成复杂的分析统计.它很像SQL中的 GROUP BY 但是功能更强大. Aggregations种类分为 ...

  6. [SQL]SQL语言入门级教材_SQL语言快速入门(五)

    SQL语言快速入门(一) SQL是英文Structured Query Language的缩写,意思为结构化查询语言. SQL语言的主要功能就是同各种数据库建立联系,进行沟通.按照ANSI(美国国家标 ...

  7. 【Oracle经典】132个oracle热门精品资料——下载目录

    电子书为网友wglzaj精心整理,这批资料下载量好评率都非常高,广受oracle学习者欢迎.文档共整理了12个精品专题和120个热门资料的下载地址,推荐给大家希望大家喜欢. 目录0豆下载地址:http ...

  8. poj 1273 (nyoj 323) Drainage Ditches : 最大流

    点击打开链接 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 49648   Accepte ...

  9. Java - Nested Classes

    (本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) Nested Classes class OuterClas ...

  10. jdk线程的死锁

    两个线程相互等着对方释放同步监听器:等着要对方的结果后才能继续执行就会发生死锁. 男对女说:你先嫁给我,我再给你买房子:女对男说:你先给我买房子,我再嫁给你. 多个线程同时锁住同一个监听对象. 在开发 ...