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

题意:对称指的是二叉树的结构上的对称。不是说某一层以上都是对称的,该层只有两个元素,分别对应上一层对称元素的两右孩子,这也是对称。这种情况的结构上不对称。如题中的反例。
方法一:维护两个队列,层次遍历,一个从左到右,一个从右到左,比较队首借点。思路上有点像判断两二叉树是否相同那题
 
/**
* Definition for binary tree
* 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==nullptr)
return true; queue<TreeNode *> Q1;
queue<TreeNode *> Q2; Q1.push(root->left);
Q2.push(root->right); while( !Q1.empty())
{
TreeNode *node1=Q1.front();
TreeNode *node2=Q2.front(); Q1.pop();
Q2.pop(); if(node1==nullptr&&node2==nullptr)
continue;
if(node1==nullptr||node2==nullptr)
return false;
if(node1->val !=node2->val)
return false; Q1.push(node1->left);
Q1.push(node1->right);
Q2.push(node2->right);
Q2.push(node2->left); }
return true;
}
}; /* //主体的另一种写法

while (!q1.empty() && !q2.empty())

{
   TreeNode *node1 = q1.front();
  TreeNode *node2 = q2.front();
  q1.pop();
  q2.pop();
  if((node1 && !node2) || (!node1 && node2)) return false;
  if (node1)

  {
    if (node1->val != node2->val) return false;
    q1.push(node1->left);
    q1.push(node1->right);
    q2.push(node2->right);
    q2.push(node2->left);
  }

}

*/

 

方法二:使用栈。特别值得注意的是:入栈的顺序,先左左,后右右,交替入栈,这样,出栈时才能保证同时从一层的两头向中间遍历。循环中,注意的条件:左右同时为0时,这种情况重新遍历即可;有一者为0,则说明不对称,返回false;对应的值不相等,也是返回false;到叶节点以后也是重新遍历未访问的结点即可。

 /**
* Definition for binary tree
* 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||(root->left==NULL&&root->right==NULL))
return true; stack<TreeNode *> stk;
stk.push(root->left);
stk.push(root->right); while( !stk.empty())
{
TreeNode *rNode=stk.top();
stk.pop();
TreeNode *lNode=stk.top();
stk.pop(); if(rNode==NULL&&lNode=NULL)
continue;
if(rNode==NULL||lNode==NULL)
return false;
if(rNode->val !=lNode->val)
return false; //判断是否到叶节点,若是返回遍历其他
if(lNode->left==NULL&&lNode->right==NULL
&&rNode->left==NULL&&rNode->right==NULL)
continue;
else
{ //注意顺序,先左左后右右,交替入栈
stk.push(lNode->left);
stk.push(rNode->right);
stk.push(lNode->right);
stk.push(rNode->left);
}
}
return true;
}
};

方法三:递归

 /**
* Definition for binary tree
* 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 subTreeSym(root->left,root->right);
} bool subTreeSym(TreeNode *lNode,TreeNode *rNode)
{
if(lNode==NULL&&rNode==NULL)
return true;
if(lNode==NULL||rNode==NULL) //和下面的那个不能颠倒顺序
return false;
if(lNode->val !=rNode->val)
return false; return subTreeSym(lNode->left,rNode->right)&&subTreeSym(lNode->right,rNode->left);
} };

[Leetcode] Symmetric tree 对称二叉树的更多相关文章

  1. 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  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(对称二叉树)

    这道题是LeetCode里的第101道题.是我在学数据结构——二叉树的时候碰见的题. 题目如下: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 ...

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

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

  5. LeetCode: Symmetric Tree 解题报告

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

  6. LeetCode【101. 对称二叉树】

    对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...

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

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

  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. 第28题:leetcode101:Symmetric Tree对称的二叉树

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

随机推荐

  1. MySQL建表

    -- 1.创建部门表dept 1 CREATE TABLE dept( 2 deptno INT PRIMARY KEY, 3 dname VARCHAR(20) UNIQUE NOT NULL, 4 ...

  2. sql语句中#{}和${}的区别

    #---将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by “111”, 如果传入的 ...

  3. python计算MD5

    python有自带的MD5模块hashlib,用起来简单很多.Python Hashlib模块的使用说明 http://docs.python.org/2/library/hashlib.htmlfd ...

  4. Linux上面安装redis和简单使用

    一.安装,redis的官方的网址   https://redis.io/ 目前的最高的版本是4.0,我安装的是2.*的版本 1.下载源码,解压后编译源码. $ wget http://download ...

  5. flask的自定义过滤器

    过滤器的本质是函数.当模板内置的过滤器不能满足需求,可以自定义过滤器.自定义过滤器有两种实现方式: 一种是通过Flask应用对象的 add_template_filter 方法 通过装饰器来实现自定义 ...

  6. Leecode刷题之旅-C语言/python-111二叉树的最小深度

    /* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minim ...

  7. u-boot.bin生成过程分析

    ELF格式“u-boot”文件的生成规则如下,下面对应Makefile的执行过程分别分析各个依赖. $(obj)u-boot: depend version $(SUBDIRS) $(OBJS) $( ...

  8. HBase 通过myeclipce脚本来获取固定columns(获取列簇中的列及对应的value值)

    第一步:关联Jar包 1. 配置hadoop-env.sh文件添加Hbase关联jar包 /opt/modules/hadoop-2.5.0-cdh5.3.6/etc/hadoop下编辑hadoop- ...

  9. PHP HashTable总结

    本篇文章主要是对 PHP HashTable 总结,下面的参考链接是很好的学习资料. 总结 HashTable 又叫做散列表,是一种用于以常数平均时间执行插入.删除和查找的技术.不能有效的支持元素之间 ...

  10. Vue 去脚手架

    上回模仿了一个nw,按照原理说,简单. 今天说Vue,脚手架是个好东西,做项目都给你配置好,不过对于我这种只想做一个界面的人来说,有点儿太大了,用不上. 如果说,不用脚手架要面临哪些问题呢. 1. 组 ...