前言

 

【LeetCode 题解】系列传送门:  http://www.cnblogs.com/double-win/category/573499.html

 

1.题目描述

 

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.


OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
/ \
2 3
/
4
\
5

The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

 

2. 题意

 

给定一颗二叉树,判断该树是否为左右对称的二叉树。

 

3. 思路

 
(1)如果一棵树仅含有一个节点,那么这个树必定是对称的。
例如:  1
(2)如果某个节点,只有左子树或者右子树,那么该树不是对称的。
例如: 1 1 1
/ 或  \  或 /    \
2 2 2  2
/  \
4    4
(3)如果某个节点,左右孩子都存在,那么递归比较:
         左孩子的左孩子 是否等于 右孩子的右孩子。
         左孩子的右孩子 是否等于 右孩子的左孩子。
 

4: 解法

class Solution {
public:
bool isSymmetric(TreeNode *root){
return root? Symmetric(root->left,root->right):true;
}
bool Symmetric(TreeNode *left, TreeNode *right){
if(left==NULL && right==NULL) return true;// 左右孩子为空
if(!left || !right) return false; // 仅含有左子树或者右子树
return left->val == right->val
&& Symmetric(left->left,right->right)
&& Symmetric(left->right,right->left);
}
};

 

作者:Double_Win

出处:  http://www.cnblogs.com/double-win/p/3891215.html

声明: 由于本人水平有限,文章在表述和代码方面如有不妥之处,欢迎批评指正~

[LeetCode 题解]: Symmetric Tree的更多相关文章

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

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

  2. [LeetCode 题解]: Binary Tree Preorder Traversal

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

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

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

  4. 【leetcode】Symmetric Tree

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

  5. Leetcode 101 Symmetric Tree 二叉树

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

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

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

  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 ...

随机推荐

  1. 学习笔记之LeetCode

    LeetCode Online Judge https://leetcode.com/ Leetcode:在线编程网站-各大IT公司的笔试面试题 http://blog.csdn.net/huixin ...

  2. Ubuntu下VIM使用指南

    基本命令: Esc:VIM中的万能功能键之一,基本上任何时候按这个键,都可以返回VIM的普通状态. i:在普通状态下按i可以进入“插入”编辑状态,这个时候按方向键移动光标,在想要输入的地方输入字符,用 ...

  3. 6.2笔记-DQL语句查询数据库

    一.DQL语句 1.常量列 SELECT `StudentName` AS 姓名, `Address` AS 地址, '北京信息中心' AS 学校名称 FROM `student`; 2.常用函数 聚 ...

  4. 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #10 Fair Group Scheduling

    HACK #10 Fair Group Scheduling 本节介绍Cgroup之一.管理CPU资源的Fair Group Scheduling.Fair Group SchedulingFair ...

  5. Linux下分析某个进程CPU占用率高的原因

      Linux下分析某个进程CPU占用率高的原因 通过top命令找出消耗资源高的线程id,利用strace命令查看该线程所有系统调用  1.top 查到占用cpu高的进程pid 2.查看该pid的线程 ...

  6. apache配置https和http的时候You don't have permission to access / on this server.

    You don't have permission to access / on this server. 是由于没有设置访问目录 今天配置httpd-ssl.conf的时候  发现这个问题 由于默认 ...

  7. module.exports 、exports、export、export default的区别

    module.exports和exports是属于 CommonJS 模块规范,export和export default是属于ES6语法. module.exports和exports导出模块,用r ...

  8. PowerDesigner软件的使用

    1. 报错:Could not Initialize JavaVM 的解决方案: powerDesigner不支持x64JDK,ok.安装32位. 仅仅是安装一下,不要做任何配置.......关闭po ...

  9. VB.NET条码机打印设置纸张大小的方法

    Imports System.Drawing.PrintingImports System.Runtime.InteropServices Public Class Page    <Runti ...

  10. How to Integrate JCaptcha in Spring Security

    The repository for JCaptcha is this one: <repository> <id>sourceforge-releases</id> ...