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. 第二次Scrum冲刺——Life in CCSU

    第二次Scrum冲刺——Life in CCSU 一. 第二次Scrum任务 继续上一次冲刺的内容,这次完成论坛部分. 二. 用户故事 用户输入账号.密码: 用户点击论坛部分: 系统显示帖子: 用户选 ...

  2. 【转】Cookie/Session机制详解

    Cookie/Session机制详解   会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息 ...

  3. Winform中Picture控件图片的拖拽显示

    注解:最近做了一个小工具,在Winform中对Picture控件有一个需求,可以通过鼠标从外部拖拽图片到控件的上,释放鼠标,显示图片! 首先你需要对你的整个Fom窗口的AllowDrop设置Ture ...

  4. 关于我与小组成员逐步升级C代码时的一些感想【第二次作业】

    #include<stdio.h> #include<stdlib.h> #include <time.h> int main(){ srand(time(NULL ...

  5. 剖析servlet injection及源码分析.

    @WebServlet("/cdiservlet") public class NewServlet extends HttpServlet { private Message m ...

  6. Python练习四

    1.任意输入一串文字加数字,统计出数字的个数,数字相连的视为一个,如:12fd2表示两个数字,即12为一个数字. content = input("请输入内容:") for i i ...

  7. c# 坑人的发邮件组件

    System.Net.Mail 在服务器25端口被封禁的情况下,无法使用其它诸如SSL 465端口发送.用过时的System.Web.Mail却可以.是微软更新速度太快呢,还是标准不一致呢. Syst ...

  8. A*搜索详解(2)——再战觐天宝匣

    书接上文.在坦克寻径的,tank_way中,A*算法每一步搜索都是选择F值最小的节点,步步为营,使得寻径的结果是最优解.在这个过程中,查找最小F值的算法复杂度是O(n),这对于小地图没什么问题,但是对 ...

  9. 2019年3月更新 技术分享 WPF基本界面制作

    1.制作流程1.在vs中建立一个wpf程序2.建立一个主页面(.cs)(注:C#程序每一个页面都由两个文件构成一个xaml一个cs,一个前端文件一个后台文件)3.在主页面中添加按钮,按钮中嵌入图片,这 ...

  10. Tcl 编译成tbc文件

    工具:tclpro1.4 下载地址:https://www.tcl.tk/software/tclpro/eval/1.4.html 永久license:  Version 1.4: 1094-320 ...