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. SOCKET.IO 的用法 系统API,

    原文:http://www.cnblogs.com/xiezhengcai/p/3956401.html 1. 服务端 io.on('connection',function(socket)); 监听 ...

  2. Resin安装配置

    在linux下安装Resin过程整理   下载Resin, http://caucho.com/products/resin/download#download   检查JDK是否安装,环境是否配置 ...

  3. 学习笔记TF055:TensorFlow神经网络简单实现一元二次函数

    TensorFlow运行方式.加载数据.定义超参数,构建网络,训练模型,评估模型.预测. 构造一个满足一元二次函数y=ax^2+b原始数据,构建最简单神经网络,包含输入层.隐藏层.输出层.Tensor ...

  4. servlet_4

    过滤器入门 过滤器的概念及执行基本流程 过滤器的使用场景 过滤器的实现及基本配置 过滤器链 过滤器链的配置 使用注解的方式无法保证过滤器链的执行顺序,所以只能使用web.xml的配置 按照出现在web ...

  5. C 标识符, 数据存储形式(原码,反码,补码)

    一.  标识符 第一个字母必须是英文字母或下划线 二. 数据存储形式(补码存储) 最高位是符号位 ---- 0表示整数 ; 1 表示负数 1. 正数:原码 = 反码 = 补码 例子 : (10) 原码 ...

  6. is_numeric — 检测变量是否为数字或数字字符串

    is_numeric — 检测变量是否为数字或数字字符串 bool is_numeric ( mixed $var ) 如果 var 是数字和数字字符串则返回 TRUE ,否则返回 FALSE . 参 ...

  7. 剑指offer 12.代码的完整性 数值的整数次方

    题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方.   本人渣渣代码: public double Power(double ba ...

  8. 通用Mapper简单使用

    通用Mapper 1.通用Mapper的使用 1.0.实体类 @Table(name = "tb_user") public class User { @Id @Generated ...

  9. 在Vue组件中获取全局的点击事件

    // 定义全局点击函数 Vue.prototype.globalClick = function (callback) { document.getElementById('main').onclic ...

  10. excel安装wps后不能正常启动

    1. HKCU\Software\Microsoft\Office\15.0\Word\Options 设置NoReReg REG_DWORD =1 2.重命名C:\Program Files (x8 ...