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 ...
随机推荐
- 设置webView头部不能滑动
设置webView头部不能滑动 _webView.scrollView.bounces=NO;
- Python开发【Django】:路由规则
Django路由规则 1.基于正则的URL 在templates目录下创建index.html.detail.html文件 <!DOCTYPE html> <html lang=&q ...
- kubernetes实战(四):k8s持久化安装rabbitmq集群
1.下载文件 https://github.com/dotbalo/k8s/ 2.创建namespace kubectl create namespace public-service 如果不使用pu ...
- Flask知识总汇
Flask基础 Flask基础使用与配置 Flask路由系统与模板系统 Flask视图函数 Flask请求与响应 Flask的session操作 Flask中间件 Flask连接数据库 Flask使用 ...
- 岭回归&Lasso回归
转自:https://blog.csdn.net/dang_boy/article/details/78504258 https://www.cnblogs.com/Belter/p/8536939. ...
- PAT 1139 First Contact[难][模拟]
1139 First Contact(30 分) Unlike in nowadays, the way that boys and girls expressing their feelings o ...
- zip和tgz以及exe的区别
在下载东西的时候总是碰见后缀是.tar.gz和.zip的问题,搞不清楚是怎么回事,不晓得下载哪个文件才是对自己有用的. 后来才知道,其实这两个压缩文件里面包含的内容是一样的,只是压缩格式不一样, ta ...
- 2.2 The Object Model -- Reopening Classes and Instances
1. 你不需要一开始定义一个类的全部内容,你可以通过使用reopen方法重新打开一个类并定义新的属性. Person.reopen({ isPerson: true }); Person.create ...
- Android ContentResolver
在Android 应用程序之间数据共享—-ContentResolver中,已经说明了Android是如何实现应用程序之间数据共享的,并详细解析了如何获取其他应用 程序共享的数据.ContentPro ...
- LCS 最长公共子序列
区别最长公共子串(连续) ''' LCS 最长公共子序列 ''' def LCS_len(x, y): m = len(x) n = len(y) dp = [[0] * (n + 1) for i ...