题目描述

给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

    1
/ \
2 2
/ \ / \
3 4 4 3

但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

    1
/ \
2 2
\ \
3 3

说明:

如果你可以运用递归和迭代两种方法解决这个问题,会很加分。

解题思路

本题可用递归和迭代两种做法来求解。

递归做法是每次对于对称的两个节点,首先判断是否都为空,若都为空则返回true;否则判断两节点是否相等,若相等则返回它们对应的子节点的比较结果;否则返回false。

迭代做法是维护一个节点队列,每次取出队列头部两个节点比较其是否相等,若不相等且两节点均不为空,则依次把两个节点对称位置的子节点加入到队列中,注意null也要加入到队列;否则返回false

代码

递归:

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

迭代:

 /**
* 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;
queue<TreeNode*> q;
q.push(root->left);
q.push(root->right);
while(q.size()){
TreeNode* node1 = q.front();
q.pop();
TreeNode* node2 = q.front();
q.pop();
if(node1 && node2){
if(node1->val != node2->val) return false;
else{
q.push(node1->left);
q.push(node2->right);
q.push(node1->right);
q.push(node2->left);
}
}
else if(node1 || node2) return false;
}
return true;
}
};

LeetCode 101. 对称二叉树(Symmetric Tree)的更多相关文章

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

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

  2. LeetCode 101 对称二叉树的几种思路(Python实现)

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

  3. [Swift]LeetCode101. 对称二叉树 | Symmetric Tree

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

  4. LeetCode 101.对称二叉树 - JavaScript

    题目描述:给定一个二叉树,检查它是否是镜像对称的. 题目分析 下面这种二叉树就是镜像对称的,符合题目要求: 1 / \ 2 2 / \ / \ 3 4 4 3 解法 1:递归检查 根据题目" ...

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

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

  6. LeetCode(25)-symmetric tree

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

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

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

  8. LeetCode(1) Symmetric Tree

    从简单的道题目開始刷题目: Symmetric Tree 题目:Given a binary tree, check whether it is a mirror of itself (ie, sym ...

  9. [Leetcode 101]判断对称树 Symmetric Tree

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

随机推荐

  1. JavaScript-->基础类型和引用类型的区别

    先了解一下数组的基础知识:附代码(数组属于引用类型的对象) <!DOCTYPE html> <html lang="en"> <head> &l ...

  2. 日志(logging)与正则(re)模块

    logging模块 #日志:日常的流水 =>日志文件,将程序运行过程中的状态或数据进行记录,一般都是记录到日志文件中 #1.logging模块一共分为五个打印级别 debug.info.warn ...

  3. h5嵌套iframe实时传参(适用vue)

    今天看到一个同事研究给iframe传参,由于好奇,我自己也写了个demo,说起来其实也挺简单的,但是在此之前没有用过,便想记录一下 其中主要用到的是postMessage 在页面中引入一个iframe ...

  4. Linux学习(一)-安装vm虚拟机以及如何在虚拟机上安装Centos系统

    (一)基本说明 学习Linux需要一个环境,我们需要创建一个虚拟机,然后在虚拟机上安装一个Centos系统来学习. 1)安装软件vm12; 2)通过vm12创建一个虚拟机空间; 3)在vm12创建好的 ...

  5. hbase实践之rowkey设计

    rowkey设计的重要性 rowkeys是HBase表设计中唯一重要的一点. rowkey设计要求 唯一性 存储特性 按照字典顺序排序存储 查询特性 由于其存储特性导致查询特性: 查询单个记录: 查定 ...

  6. SQL拼串--小问题大毛病

    用字符串拼接SQL串实现动态SQL语句,经常使用. 不过在拼接SQL串中有许多技巧和小陷阱 1.遗漏""和'' 错误案例: 错误结果: 发现没有数据产生..我还以为是连接错了数据库 ...

  7. Python3-BaseType

    counter = 100 # 整型变量 miles = 1000.0 # 浮点型变量 name = "runoob" # 字符串 print(counter) print(mil ...

  8. vs2015下载

    VS2015 专业版下载链接http://download.microsoft.com/download/B/8/9/B898E46E-CBAE-4045-A8E2-2D33DD36F3C4/vs20 ...

  9. Centos7 Memcached 安装

    1.Linux系统安装memcached,首先要先安装libevent库. yum install libevent libevent-devel 2.安装memcached yum install ...

  10. 5、docker容器数据卷: -v添加共享传递容器数据卷

    1.是什么 1.docker理念 先来看看Docker的理念:*  将运用与运行的环境打包形成容器运行 ,运行可以伴随着容器,但是我们对数据的要求希望是持久化的*  容器之间希望有可能共享数据 2.保 ...