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

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

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

But the following [1,2,2,null,3,null,3] is not:

   / \

   \   \
       

Note:
Bonus points if you could solve it both recursively and iteratively.

判断是否是对称树和判断两棵树是否相同是一样的思路。

第一种方法:使用递归(C++)

     bool leftEqualRight(TreeNode* left,TreeNode* right){
if(!left&&!right)
return true;
if((!left&&right)||(left&&!right)||(left->val!=right->val))
return false;
return leftEqualRight(left->left,right->right)&&leftEqualRight(left->right,right->left);
} bool isSymmetric(TreeNode* root) {
if(!root)
return true;
return leftEqualRight(root->left,root->right);
}

第二种方法:使用迭代(C++),利用两个队列来分别存储根节点的左、右子树。首先,根节点为空,则对称,将根节点的左右子树分别压入两个队列,循环判断的条件是两个队列都不为空,当两个出队的结点都为空时,continue跳过此次判断,继续进行,当其中一个节点为空时,或者两个结点的值不相等时,跳出,false,再分别将两个结点的子树压入,左子树的左结点对应右子树的右结点,左子树的右结点对应右子树的左结点。

 bool isSymmetric(TreeNode* root) {
if(!root)
return true;
queue<TreeNode*> q1,q2;
q1.push(root->left);
q2.push(root->right);
while(!q1.empty()&&!q2.empty()){
TreeNode* node1=q1.front();
q1.pop();
TreeNode* node2=q2.front();
q2.pop();
if(!node1&&!node2)
continue;
if((!node1&&node2)||(node1&&!node2)||(node1->val!=node2->val))
return false;
q1.push(node1->left);
q2.push(node2->right);
q1.push(node1->right);
q2.push(node2->left);
}
return true;
}

LeetCode 101. Symmetric Tree 判断对称树 C++的更多相关文章

  1. LeetCode 101. Symmetric Tree (对称树)

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

  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 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

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

  4. LeetCode 101. Symmetric Tree(镜像树)

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

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

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

  6. Leetcode 101 Symmetric Tree 二叉树

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

  7. LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)

      思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...

  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. 101. Symmetric Tree -- 判断树结构是否对称

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

随机推荐

  1. Python中的常用魔术方法介绍

    1.__init__ 初始化魔术方法 触发时机:初始化对象时触发(不是实例化触发,但是和实例化在一个操作中) 参数:至少有一个self,接收对象 返回值:无 作用:初始化对象的成员 注意:使用该方式初 ...

  2. ASP.NET+MVC+EntityFramework快速实现增删改查

    本教程已经录制视频,欢迎大家观看我在CSDN学院录制的课程:http://edu.csdn.net/lecturer/944

  3. laravel seed填充数据步骤

  4. Spring的由来以及发展

    Spring简史: 第一阶段:XML配置在Spring1.x时代,都是使用XML配置Bean,随着项目扩大,我们把XML文件切分成多个配置文件,那时候需要频繁地在开发类和配置文件之间切换    第二阶 ...

  5. C++中关于字符串的一些API

    参考资料:http://www.runoob.com/cplusplus/cpp-strings.html 一.前言 不管是在编写何种语言程序,需要从输入设备中获取数据的需求很频繁,在这类频繁的应用场 ...

  6. maya_help()验证编程过程中模块导入的情况

    import rigLib reload(rigLib.base.control)spine = rigLib.base.control.Control( prefix = 'spine1') hel ...

  7. axios delete

  8. chrome添加离线插件

    1.首先用户点击谷歌浏览器右上角的自定义及控制按钮,在下拉框中选择工具选项,然后点击扩展程序来启动Chrome浏览器的扩展管理器页面. 2.在打开的谷歌浏览器的扩展管理器中用户可以看到一些已经安装程序 ...

  9. myql 格式化日期

    date_format(a.balance_date,'%Y-%m')= date_format(#{balanceDate},'%Y-%m')

  10. chrome浏览器onunload方法无法执行window.location.href

    记录用户不正常退出,如关闭浏览器的时候,执行onunload方法,跳回后台记录用户已经退出的信息,在ie上可以正常跳转,但在Firefox和chrome上却无法跳转. 测试后发现以下方法可以实现,支持 ...