</pre>Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).<p></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">For example, this binary tree is symmetric:</p><pre style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857143; color: rgb(51, 51, 51); word-break: break-all; word-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px;">    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.

/**
* 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 left_right_Symmetric(TreeNode* pleft,TreeNode* pright)
{
if(pleft==NULL&&pright==NULL) return true;
if(pleft==NULL&&pright!=NULL)return false;
if(pleft!=NULL&&pright==NULL)return false;
if(pleft->val!=pright->val) return false;
return left_right_Symmetric(pleft->left,pright->right)&&left_right_Symmetric(pleft->right,pright->left);
}
public:
bool isSymmetric(TreeNode* root) {
if(root == NULL) return true;
if(root->left!=NULL&&root->right==NULL) return false;
if(root->left==NULL&&root->right!=NULL) return false;
if(root->left!=NULL&&root->right!=NULL&&root->left->val!=root->right->val)return false;
else return left_right_Symmetric(root->left,root->right);
}
};

LeetCode之Symmetric Tree的更多相关文章

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

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

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

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

  3. 【leetcode】Symmetric Tree

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

  4. Leetcode 101 Symmetric Tree 二叉树

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

  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] 10. Symmetric Tree

    这次我觉得我的智商太低,想了很久才写出来.题目是让求镜像二叉树判断,题目如下: Given a binary tree, check whether it is a mirror of itself ...

  8. [LeetCode 题解]: Symmetric Tree

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述   Given a ...

  9. leetcode 101 Symmetric Tree ----- java

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

  10. LeetCode 101. Symmetric Tree

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

随机推荐

  1. Windows版变色龙

    打包安装版本更新源地址: http://www.insanelymac.com/forum/files/file/59-chameleon-22-svn/ 一.使用方法:1.安装Windows版变色龙 ...

  2. django 使用post方法出现403错误的解决办法

    当采用客户端象django的服务器提交post请求时.会得到403,权限异常.因为django针对提交的请教,有校验.所以会如此. 解决办法: 导入模块:from django.views.decor ...

  3. mysql5.5和5.6版本间的坑

    mysql 5.5 int类型 设置不为null,无填充,添加新数据会自动填充0 而5.6同样的配置新建数据没值时,不让添加 5.5 datetime 不能设置默认时间(可以通过某些复杂的方式,这里说 ...

  4. 用swift开发仪表盘控件(二)

    二.代码分析 这个控件本质就是从UIView继承的一个类而已.所以整个代码事实上就是一个定制的UIView类. 依据UIView的规则进行例如以下初始化: required init(coder aD ...

  5. [Application]Ctrl+C终止程序代码

    代码如下: #include <stdio.h> #include <stdlib.h> #include <iostream> #include <sign ...

  6. http协议之报文详解

    一. 概述 用于HTTP协议交互的信息被称为HTTP报文.请求端(客户端)的http报文叫做请求报文,响应端的叫做响应报文. 报文,是网络中交换和传输的数据单元,即站点一次性要发送的数据块.报文包含了 ...

  7. Android学习之两款下拉刷新库分享

    昨天没有写博客.心里非常罪过呀,今天给大家写两种比較常见的下拉刷新的用法.一款是SwipeRefreshLayout,一款是CircleRefreshLayout. SwipeRefreshLayou ...

  8. Spring.Net框架一:Spring.Net简介

    一.Spring.Net简介 Spring.NET为建立企业级应用提供了一套轻量级的解决方案.通过Spring.NET,我们可以用统一且透明的方式来配置应用程序.Spring.NET的重点是为中间层提 ...

  9. PostgreSQL入门指南

    默认端口:5432 创建用户与数据库 PostgreSQL初次安装后,默认会生成一个名为postgres的数据库和一个名为postgres的数据库用户.这里需要注意的是,同时还生成了一个名为postg ...

  10. heartbeat 心跳技术

    转自:http://blog.csdn.net/keda8997110/article/details/8349049 heartbeat 心跳技术原理: heartbeat (Linux-HA)的工 ...