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 ...
随机推荐
- Python中的常用魔术方法介绍
1.__init__ 初始化魔术方法 触发时机:初始化对象时触发(不是实例化触发,但是和实例化在一个操作中) 参数:至少有一个self,接收对象 返回值:无 作用:初始化对象的成员 注意:使用该方式初 ...
- jsp(待改)
##JSP 1.指令 作用:用于配置JSP页面,导入资源文件 *书写格式 <%@ 指令名称 属性1=值1,属性2=值2 ...%> *分类: *page :配置JSP页面的 #属性: c ...
- 21. Wireless tools (无线工具 5个)
AircrackKismetNetStumblerinSSIDerKisMAC
- 芯灵思Sinlinx A64 开发板移植SQLite3
开发平台 芯灵思Sinlinx A64 内存: 1GB 存储: 4GB 开发板详细参数 https://m.tb.cn/h.3wMaSKm 开发板交流群 641395230 首先到 http://ww ...
- C++ 自定义时间
今天精神状态不好,和公司的领导请了假.为了抵抗我的痛苦,我在床上打坐冥想,从早上九点到下午三点二十六.嗯,感觉好多了.这种温和的暴力果然有效. 之后吃了点东西,然后无聊的我就在想,明天的工作该 ...
- PythonStudy——函数对象的案例
# part1 # 加法运算 def add(n1, n2): return n1 + n2 def low(n1, n2): return n1 - n2 # 四则运算 def computed(n ...
- 现在企业开发时,Java所用到的主流框架有哪些?
虽然Java一直被唱衰,但是直到现在Java软件开发也坚持霸主地位不动摇.毫无疑问,Java是目前最热门的编程语言之一.随着Java面向对象语言的流行以及多层架构应用的出现,使得应用程序的可复用性得到 ...
- Vector Math for 3D Computer Graphics (Bradley Kjell 著)
https://chortle.ccsu.edu/VectorLessons/index.html Chapter0 Points and Lines (已看) Chapter1 Vectors, P ...
- 在kerberos认证过程中Active Directory的作用
LDAP介绍 1),ladp(Lightweight Directory Access Protocol),轻量级目录访问协议,提供被称为目录服务的信息服务,特别是基于X.500(构成全球分布式的目录 ...
- Python(四) —— 函数
什么是函数? 把一堆代码放一起就叫函数 函数用来干什么? 不复写代码,提高代码重复利用程度 怎么定义以及调用函数: def fun1(): #定义函数 print('这是一个函数') #函数体,里面什 ...