101. Symmetric Tree -- 判断树结构是否对称
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
1
/ \
2 2
/ \ / \
3 4 4 3
But the following is not:
1
/ \
2 2
\ \
3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
1. 递归
bool isSymmetric(TreeNode *p, TreeNode *q){
if (p==NULL && q==NULL) return true;
if (p==NULL || q==NULL) return false;
return (p->val == q->val) &&
isSymmetric(p->left, q->right) &&
isSymmetric(p->right, q->left);
}
2. 非递归
bool isSymmetric(TreeNode *p, TreeNode *q)
{
queue<TreeNode*> q1;
queue<TreeNode*> q2;
q1.push(p);
q2.push(q);
while(q1.size()> && q2.size()>){
TreeNode* p1 = q1.front();
q1.pop();
TreeNode* p2 = q2.front();
q2.pop();
if (p1==NULL && p2==NULL) continue;
if (p1==NULL || p2==NULL) return false; if (p1->val != p2->val) return false; q1.push(p1->left);
q2.push(p2->right); q1.push(p1->right);
q2.push(p2->left); }
return true;
}
101. Symmetric Tree -- 判断树结构是否对称的更多相关文章
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)
思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...
- 二叉树系列 - [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 ...
- 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称).例如,这个二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \3 4 4 3但是 ...
- [leetcode] 101. Symmetric Tree 对称树
题目大意 #!/usr/bin/env python # coding=utf-8 # Date: 2018-08-30 """ https://leetcode.com ...
- Leetcode之101. Symmetric Tree Easy
Leetcode 101. Symmetric Tree Easy Given a binary tree, check whether it is a mirror of itself (ie, s ...
- <LeetCode OJ> 101. Symmetric Tree
101. Symmetric Tree My Submissions Question Total Accepted: 90196 Total Submissions: 273390 Difficul ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
随机推荐
- talib 中文文档(十):Price Transform Functions 价格指标
Price Transform Functions AVGPRICE - Average Price 函数名:AVGPRICE 名称:平均价格函数 real = AVGPRICE(open, high ...
- Girls and Boys---hdu1068(最大独立集=顶点数-最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1068 题意:有n个人,他们之间存在着恋爱关系,现在告诉你每个人和其他人的关系,然后要从这n个人间选出尽 ...
- day13(JSTL和自定义标签&MVC模型&javaweb三层框架)
day13 JSTL标签库(重点) 自定义标签(理解) MVC设计模式(重点中的重点) Java三层框架(重点中的重点) JSTL标签库 1 什么是JSTL JSTL是apache对EL表达式的扩 ...
- 【Loadrunner】使用LR录制HTTPS协议的三种方法
使用LR录制HTTPS协议的三种方法 一.最简单的方法:浏览器配置打开浏览器,安装证书,配置完成后直接用http协议录制即可(配置完成的标识就是打开网页,不显示安全提示) 二.LR配置修改操作步骤如下 ...
- SaltStack系列(一)之环境部署、命令及配置文件详解
一.SaltStack介绍 1.1 saltstack简介: saltstack是基于python开发的一套C/S架构配置管理工具,它的底层使用ZeroMQ消息队列pub/sub方式通信,使用SSL证 ...
- 使用Ajax验证用户是否已存在
在服务器端使用Servlet,里面在集合里存了几个字符串,没有对数据库操作. 前台input页面和Ajax验证: <%@ page language="java" conte ...
- uva1401 dp+Trie
这题说的是给了一个长的字符串长度最大300000,又给了4000个单词 单词的长度不超过100.计算这个字符串能组成多少种不同单词的组合,求出方案总数.dp[i]以第i个字符为开始的字符串能有多少种的 ...
- iOS重签名脚本
unzip xxx.ipa //解压ipa rm -rf Payload/ xxx.app/_CodeSignature //删除旧签名 cp newEmbedded.mobileprovision ...
- Java中com.jcraft.jsch.ChannelSftp讲解
http://blog.csdn.net/allen_zhao_2012/article/details/7941631 http://www.cnblogs.com/longyg/archive/2 ...
- monit拉起服务
check process hive_metastore matching "HiveMetaStore" start program = "/usr/bin/nohup ...