LeetCode 101. Symmetric Tree 判断对称树 C++
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++的更多相关文章
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [LeetCode] Symmetric Tree 判断对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For ex ...
- LeetCode 101. Symmetric Tree(镜像树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- Leetcode 101 Symmetric Tree 二叉树
判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert B ...
- LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)
思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...
- [leetcode]101. Symmetric Tree对称树
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 101. Symmetric Tree -- 判断树结构是否对称
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- C++实验五
#include <iostream> #include <vector> #include <string> using namespace std; // 函数 ...
- sql执行内部操作期间检测到不一致性解决方案
解决方法:重启下SQL服务,把下面脚本运行即可.运行后,坏掉的数据库可能会丢失. --mydb 为坏了的数据库名--mytable 为坏了的据库表--master 这里不需要更改 use mydb ...
- Python 配置 selenium 模拟浏览器环境,带下载链接
使用浏览器渲染引擎.直接用浏览器在显示网页时解析HTML,应用CSS样式并执行JavaScript的语句. 这方法在爬虫过程中会打开一个浏览器,加载该网页,自动操作浏览器浏览各个网页,顺便把数据抓下来 ...
- robotframework之去除空格、去掉前面的0、增加空格换行符的方法,两个字符之间的拼接
1.去除空格 A)若需要去除两个拼接字符之间的空格,可以使用robotframework中的关键词Catenate,需要注意的是SEPARATOR=一定需要大写 B)若在一个字符串中存在空格,且需要去 ...
- Linux第十一节课学习笔记
区域就是firewalld预先准备了几套防火墙策略集合(策略模板),用户可以根据生产场景的不同而选择合适的策略集合,从而实现防火墙策略之间的快速切换. RUNTIME模式:当前生效,重启失效:PERM ...
- WinForm控件开发总结目录
WinForm控件开发总结(一)------开篇 WinForm控件开发总结(二)------使用和调试自定义控件 WinForm控件开发总结(三)------认识WinForm控件常用的Attrib ...
- C#集合类型大揭秘 【转载】
[地址]https://www.cnblogs.com/songwenjie/p/9185790.html 集合是.NET FCL(Framework Class Library)的重要组成部分,我们 ...
- springmvc的dispatchservlet初始化
初始化做的事情,处理下controller的映射关系 https://blog.csdn.net/qq_38410730/article/details/79426673
- Spring Boot 全局异常处理
Spring Boot版本 1.5 @ControllerAdvice public class GlobalExceptionHandler extends ResponseEntityExcept ...
- SpringMVC的HandlerMapping(处理器映射器)
SpringMvc有四种HandlerMapping:BeanNameUrlHandlerMapping ,SimpleUrlHandlerMapping,ControllerClassNameHan ...