题目:

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.

分类:Tree BFS DFS

代码:

 /**
* 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) return true;
return helper(root->left, root->right);
} bool helper(TreeNode* p, TreeNode* q) {
if (!p && !q) {
return true;
} else if (!p || !q) {
return false;
} if (p->val != q->val) {
return false;
} return helper(p->left,q->right) && helper(p->right, q->left);
}
};

[LeetCode101]Symmetric Tree的更多相关文章

  1. 第28题:leetcode101:Symmetric Tree对称的二叉树

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

  2. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  3. 【leetcode】Symmetric Tree

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

  4. 38. Same Tree && Symmetric Tree

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

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

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

  6. LeetCode之“树”:Symmetric Tree && Same Tree

    Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...

  7. LeetCode: Symmetric Tree 解题报告

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

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

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

  9. 【LeetCode】101. Symmetric Tree (2 solutions)

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

随机推荐

  1. PVPlayer的实现方式

    关于opencore下多媒体播放,在mediaserver进程里面仅仅有一行代码: MediaPlayerService::instantiate(); 这行代码的作用是初始化一个MediaPlaye ...

  2. 模拟spring框架注入实现原理

    这个我是参见了别人的一些东西,不是原创! 定义一些抽象的方法: package com.huxin.springinject.dao; public interface Person { public ...

  3. hdu4280(最大流)

    传送门:Island Transport 题意:有N个岛屿 M条无向路 每个路有一最大允许的客流量,求从最西的那个岛屿最多能运用多少乘客到最东的那个岛屿. 分析:无向图正反都加弧,权值一样,这题点多, ...

  4. Erlangserver紧内存优化解决方案

    提出的问题:server100万人在线,16G内存快被吃光. 玩家进程占用内存偏高 解决方法: 第一步: erlang:system_info(process_count). 查看进程数目是否正常,是 ...

  5. Android设计模式(二)--策略模式

    1.定义: The Strategy Pattern defines a family of algorithms,encapsulates each one,and makes them inter ...

  6. hdu1992(递推)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1992 题意:用1*2和2*1的小长方形铺垫4*W的方格有多少种方法. 分析:假如新加入1列,这列都竖着 ...

  7. 自己做站点(二) 20块钱搞定一个企业站:域名&amp;空间申请

    域名注冊的话,推荐大家用新网,由于申请费用确实非常低,但续费的价格还是比較高的,所以不妨多申请几年.打开站点: http://www.xinnet.com/ 注冊一个帐号,然后申请域名,你能够看到,费 ...

  8. Android5.0L下因sensorservice crash导致systemserver重新启动的第二种场景分析

    一.出问题的场景 1.Sensorservice线程正在处理compass sensor事件的过程中.检查了一次buffer的指针的有效性,并在稍后会传递到AKM获取数据的函数接口中使用 2.Sens ...

  9. 基于Redis Sentinel的Redis集群(主从Sharding)高可用方案(转)

    本文主要介绍一种通过Jedis&Sentinel实现Redis集群高可用方案,该方案需要使用Jedis2.2.2及以上版本(强制),Redis2.8及以上版本(可选,Sentinel最早出现在 ...

  10. 13.怎样自学Struts2之Struts2本地化[视频]

    13.怎样自学Struts2之Struts2本地化[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了,仅仅好传到百度云上: http://pan ...