【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 trees are considered equal if they are structurally identical and the nodes have the same value.
解法一:递归
/**
* 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 isSameTree(TreeNode *p, TreeNode *q) {
if(!p && !q)
return true;
else if(!p && q)
return false;
else if(p && !q)
return false;
else
{
if(p->val != q->val)
return false;
else
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
}
};
解法二:非递归
建立两个队列分别进行层次遍历,进队时检查对应点是否相等
/**
* 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 isSameTree(TreeNode *p, TreeNode *q) {
if(!isSameNode(p, q))
return false;
if(!p && !q)
return true; queue<TreeNode*> lqueue;
queue<TreeNode*> rqueue;
lqueue.push(p);
rqueue.push(q);
while(!lqueue.empty() && !rqueue.empty())
{
TreeNode* lfront = lqueue.front();
TreeNode* rfront = rqueue.front(); lqueue.pop();
rqueue.pop(); if(!isSameNode(lfront->left, rfront->left))
return false;
if(lfront->left && rfront->left)
{
lqueue.push(lfront->left);
rqueue.push(rfront->left);
} if(!isSameNode(lfront->right, rfront->right))
return false;
if(lfront->right && rfront->right)
{
lqueue.push(lfront->right);
rqueue.push(rfront->right);
}
}
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】100. Same Tree (2 solutions)的更多相关文章
- 【LeetCode】100. Same Tree 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...
- 【LeetCode】100 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【LeetCode】101. Symmetric Tree (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【一天一道LeetCode】#100. Same Tree(100题大关)
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
随机推荐
- nodejs 导入导出模块module.exports向外暴露多个模块 require导入模块
.moudel.exports 导出模块 导出单个模块 // user.js moudel.exports = 函数名或者变量名: //app.js 导入 require('user.js') 当然. ...
- 利用站点ip引导提高站点权重的可行方案
如题,利用站点每天高数额的ip訪问量来提高站点权重,首先在谈论这个话题之前,我举个样例.我们知道想要一个站点权重非常高,首先它站点本身的内容一定是有价值的,而且受大众欢迎的,人们会常常訪问这个站点来寻 ...
- 用SoapUI进行Webservice的性能压力测试
转载:http://www.cnblogs.com/fnng/archive/2011/08/11/2135440.html 第一步: 新建一个项目:点击新建按钮就行了. 在打开的窗口中填写你项目名, ...
- this与JavaScrip中的四种调用模式
this是什么 方法调用模式 构造器调用模式 函数调用模式 apply/call模式 this是什么 —In most languages, ‘this’ is a reference to the ...
- .NET Versioning and Multi-Targeting - .NET 4.5 is an in-place upgrade to .NET 4.0
Say what you will about the past ridiculousness of .NET Framework versioning, since the confusion of ...
- 安装sql2012 正在启动操作系统功能"NetFx3"
安装完windows8 后开始安装sql2012,安装过程中停在“正在启动操作系统功能"NetFx3"”不动了,很是着急,于是上网查了一下资料,原来NetFx3指的是Framewo ...
- java集合(ArrayList,Vector,LinkedList,HashSet,TreeSet的功能详解)
说起集合,我们会潜意识里想到另外一个与之相近的名词——数组,OK!两者确实有相似之处,但也正是这点才是我们应该注意的地方,下面简单列出了两者的区别(具体功能的不同学习这篇文章后就会明白了): 数组 长 ...
- telnet用法 测试端口号
Telnet是进行远程登录的标准协议和主要方式它为用户提供了在本地计算机上完成远程主机工作的能力.可以用telnet命令来测试端口号是否正常打开还是关闭状态. 工具/原料 电脑 cmd命令 方法/步骤 ...
- 2017年USNews美国大学研究生专业排名
2017年USNEWS美国大学研究生专业排名最佳商学院排名 排名 学校 费用 注册人数 #1 Harvard University Boston, MA $61,225 per year (full- ...
- Navicat工具里的empty table和truncate table的区别
如图: 相同点:都会清空数据表里的所有数据 不同点:empty table是清空表里的数据:truncate table是删除表,然后再创建这张表 意义:对于主索引自动增加的情况,empty清表后,新 ...