二叉树系列 - [LeetCode] 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.
二叉树是否对称的本质,其实是判定两棵树是否镜像。
递归是很常见的实现方式,最简便。
/**
* 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 compRoot(root -> left, root -> right);
}
private:
bool compRoot(TreeNode* lroot, TreeNode* rroot){
if(!lroot) return (NULL == rroot);
if(NULL == rroot) return false;
if(lroot -> val != rroot -> val) return false;
return (compRoot(lroot -> left, rroot -> right) && compRoot(lroot -> right, rroot -> left));
}
};
非递归,我的方法其实还是很常规,用栈来代替。因为是对称比较,所以要两个栈。
这个思路其实可以稍微简化一下,改用一个双端队列deque实现。比起用两个栈来,显得稍微“洋气”一点 ==。
/**
* 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(!root -> left && !root -> right) return true;
if( (!root -> left && root -> right) || (root -> left && !root -> right) ) return false;
deque<TreeNode*> dq;
dq.push_front(root -> left);
dq.push_back(root -> right);
while(!dq.empty()){
TreeNode* lroot = dq.front();
TreeNode* rroot = dq.back();
dq.pop_front();
dq.pop_back();
if(lroot -> val != rroot -> val) return false;
if( (!lroot -> right && rroot -> left) || (lroot -> right && !rroot -> left) ) return false;
if(lroot -> right){
dq.push_front(lroot -> right);
dq.push_back(rroot -> left);
}
if( (!lroot -> left && rroot -> right) || (lroot -> left && !rroot -> right) ) return false;
if(lroot -> left){
dq.push_front(lroot -> left);
dq.push_back(rroot -> right);
}
}
return true;
}
};
二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现的更多相关文章
- [LeetCode] 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 ...
- LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)
思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...
- LeetCode: Symmetric Tree 解题报告
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 数据结构二叉树的递归与非递归遍历之java,javascript,php实现可编译(1)java
前一段时间,学习数据结构的各种算法,概念不难理解,只是被C++的指针给弄的犯糊涂,于是用java,web,javascript,分别去实现数据结构的各种算法. 二叉树的遍历,本分享只是以二叉树中的先序 ...
- C实现二叉树(模块化集成,遍历的递归与非递归实现)
C实现二叉树模块化集成 实验源码介绍(源代码的总体介绍):header.h : 头文件链栈,循环队列,二叉树的结构声明和相关函数的声明.LinkStack.c : 链栈的相关操作函数定义.Queue. ...
- 二叉树3种递归和非递归遍历(Java)
import java.util.Stack; //二叉树3种递归和非递归遍历(Java) public class Traverse { /******************一二进制树的定义*** ...
- JAVA递归、非递归遍历二叉树(转)
原文链接: JAVA递归.非递归遍历二叉树 import java.util.Stack; import java.util.HashMap; public class BinTree { priva ...
- 二叉树前中后/层次遍历的递归与非递归形式(c++)
/* 二叉树前中后/层次遍历的递归与非递归形式 */ //*************** void preOrder1(BinaryTreeNode* pRoot) { if(pRoot==NULL) ...
随机推荐
- leetcode个人题解——#24 Swap Nodes in Pairs
因为不太熟悉链表操作,所以解决方法烦了点,空间时间多有冗余. 代码中l,r分别是每一组的需要交换的左右指针,temp是下一组的头指针,用于交换后链接:res是交换后的l指针,用于本组交换后尾指针在下一 ...
- nginx配置和网站的部署
环境: CentOS Linux release 7.3.1611 (Core) nginx version: nginx/1.13.4 PHP 5.4.16 (cli) (built: Nov 6 ...
- 求gcd(最大公因数),lcm(最小公倍数)模板
gcd(最大公因数),lcm(最小公倍数) #include<iostream> using namespace std; int gcd(int a,int b)//辗转相除法(欧几里德 ...
- oracle 9i 图文安装教程 oracle 9i 安装
我的安装文件是ISO镜像文件,使用Virtual DAEMON Manager v 4.10打开: ora9i-1.iso ora9i-2.iso ora9i-3.iso 首先必须把上面三个镜像文件都 ...
- ManagementClass("Win32_Share")之共享目录
public class ShareFolder { private static readonly Dictionary<uint, string> ReturnDetails = ne ...
- DELPHI dbgrid 选中的是第几行 怎么判断?
使用DataSource.DataSet.RecNo可以得到dbgrid选中的是第几行,示例代码如下: procedure TForm1.btn1Click(Sender: TObject); beg ...
- window 安装 nvm
下载地址 https://github.com/coreybutler/nvm-windows/releases 设置淘宝镜像 nvm node_mirror https://npm.taobao.o ...
- 【Python】Python简易爬虫爬取百度贴吧图片
通过python 来实现这样一个简单的爬虫功能,把我们想要的图片爬取到本地.(Python版本为3.6.0) 一.获取整个页面数据 def getHtml(url): page=urllib.requ ...
- MATLAB strcmp
比较两个输入字符串是否相等 c = strcmp(str1,str2)比较字符串 str1 与 str2 ,若完全相等则返回 1 ,不相等返回 0 str1 = 'hello'; str2 = 'he ...
- 可视化自编码器训练结果&稀疏自编码器符号一览表
训练完(稀疏)自编码器,我们还想把这自编码器学习到的函数可视化出来,好弄明白它到底学到了什么.我们以在10×10图像(即n=100)上训练自编码器为例.在该自编码器中,每个隐藏单元i对如下关于输入的函 ...