【LeetCode】101. Symmetric Tree (2 solutions)
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、left对应right
2、left->left对应right->right
3、left->right对应right->left
解法一:递归
- /**
- * Definition for binary tree
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- bool isSymmetric(TreeNode *root) {
- if(!root)
- return true;
- return isSymTree(root->left, root->right);
- }
- bool isSymTree(TreeNode* p, TreeNode* q)
- {
- if(!isSameNode(p, q))
- return false;
- if(!p && !q)
- return true;
- return isSymTree(p->left, q->right) && isSymTree(p->right, q->left);
- }
- bool isSameNode(TreeNode* p, TreeNode* q)
- {
- if(!p && !q)
- return true;
- if((!p && q) || (p && !q) || (p->val != q->val))
- return false;
- return true;
- }
- };
解法二:非递归
使用两个队列,对左右子树分别进行层次遍历。
进队时的对应元素比较即可。
- /**
- * Definition for binary tree
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- bool isSymmetric(TreeNode *root) {
- if(!root)
- return true;
- if(!isSameNode(root->left, root->right))
- return false;
- if(!root->left && !root->right)
- return true;
- queue<TreeNode*> lqueue;
- queue<TreeNode*> rqueue;
- lqueue.push(root->left);
- rqueue.push(root->right);
- while(!lqueue.empty() && !rqueue.empty())
- {
- TreeNode* lfront = lqueue.front();
- TreeNode* rfront = rqueue.front();
- lqueue.pop();
- rqueue.pop();
- if(!isSameNode(lfront->left, rfront->right))
- return false;
- if(lfront->left && rfront->right)
- {
- lqueue.push(lfront->left);
- rqueue.push(rfront->right);
- }
- if(!isSameNode(lfront->right, rfront->left))
- return false;
- if(lfront->right && rfront->left)
- {
- lqueue.push(lfront->right);
- rqueue.push(rfront->left);
- }
- }
- return true;
- }
- bool isSameNode(TreeNode* p, TreeNode* q)
- {
- if(!p && !q)
- return true;
- if((!p && q) || (p && !q) || (p->val != q->val))
- return false;
- return true;
- }
- };
【LeetCode】101. Symmetric Tree (2 solutions)的更多相关文章
- 【LeetCode】101. Symmetric Tree 对称二叉树(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...
- 【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
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【easy】101. Symmetric Tree
判断一棵二叉树是否对称 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left ...
- 【LeetCode】100. Same Tree (2 solutions)
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 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】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- iOS 画圆
_demoView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; [self.view addSubview:_de ...
- ROS知识(6)----基于Eclipse开发
可以利用Eclipse集成开发环境进行ROS开发,从而提高研发效率.以色列巴尔伊兰大学的Mr. Roi Yehoshua开设了一门ROS课程,课程2( Lesson 2)讲解了如何利用Eclipse在 ...
- linuxmint - setup - 搜狗输入法
安装好linuxmint18后,官网下载搜狗输入法安装包安装.安装成功后,发现缺失部分界面,包括输入候选框,软件设置,fcitx设置都不太正常. 解决: 安装:fcitx-ui-classic 另: ...
- mysql表前缀
之前一直没明白,mysql有些规范里面,建议建表的时候添加前缀,它的意义究竟是为何.直到最近,我想学习一下Swift的网络请求,于是打算在新浪云新建个项目却发现新浪云免费用户最多只能建立5个项目.于是 ...
- 更改CentOS 6.3 yum源为国内 阿里云源
将CentOS的 yum源 更换为 阿里云源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.b ...
- python接口自动化9-https请求(SSL)
前言 本来最新的requests库V2.13.0是支持https请求的,但是一般写脚本时候,我们会用抓包工具fiddler,这时候会报:requests.exceptions.SSLError: [S ...
- N个富文本编辑器/基于Web的HTML编辑器
转自:http://www.cnblogs.com/lingyuan/archive/2010/11/15/1877447.html 基于WEB的HTML 编辑器,WYSIWYG所见即所得的编辑器,或 ...
- U3D内存优化
原创文章如需转载请注明:转载自风宇冲Unity3D教程学院 U3D内存优化 读了Hog关于内存管理文章 ...
- OpenCV定制化创建角点检测子
定制化创建角点检测子 目标 在这个教程中我们将涉及: 使用 OpenCV 函数 cornerEigenValsAndVecs 来计算像素对应的本征值和本征向量来确定其是否是角点. 使用OpenCV 函 ...
- 用IIS防止mdb数据库被下载
如何防止mdb数据库被下载?本文讨论的是在服务器端禁止mdb格式数据库文件被下载,而不是在数据库中加入防下载表,将数据库名改为含#号的asp.asa等后缀格式. 下面以IIS6.0为例说明如何在服务器 ...