LeetCode OJ: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.
判断两个树是否相同,起是用递归很好解决,但是我一开始居然想使用前序加中序遍历来唯一确定一棵树:
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
vector<int>res1;
vector<int>res2;
if(p == NULL && q == NULL)
return true;
else if(p == NULL || q == NULL)
return false;
preorderTraversal(p, res1);
preorderTraversal(q, res2);
bool ans1 = res1 == res2;
res1.clear(), res2.clear();
infixTraversal(p, res1);
infixTraversal(q, res2);
bool ans2 = res1 == res2;
return res1 && res2;
}
void preorderTraversal(TreeNode * p, vector<int> & res)
{
if(p == NULL) return;
res.push_back(p->val);
preorderTraversal(p->left, res);
preorderTraversal(p->right, res);
}
void infixTraversal(TreeNode * p, vector<int> & res)
{
if(p == NULL) return;
infixTraversal(p->left, res);
res.push_back(p->val);
infixTraversal(p->right, res);
}
};
然而并不可行,只能使用下面这一种方法:
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 a binary tree node.
* 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) {
queue<TreeNode *> pQueue;
queue<TreeNode *> qQueue;
pQueue.push(p); //这里只能用队列而不能用栈,因为单向链表的性质决定
qQueue.push(q);
while(!pQueue.empty() && !qQueue.empty()){
TreeNode * pTmp = pQueue.front();
TreeNode * qTmp = qQueue.front();
pQueue.pop(), qQueue.pop();
if(!pTmp && !qTmp)
continue;
else if(pTmp && !qTmp || !pTmp && qTmp || pTmp->val != qTmp->val)
return false;
else{
pQueue.push(pTmp->left);
pQueue.push(pTmp->right);
qQueue.push(qTmp->left);
qQueue.push(qTmp->right);
}
}
return pQueue.empty() && qQueue.empty();
}
};
下面是java代码:
递归的:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null)
return true;
else if(p == null)
return false;
else if(q == null)
return false;
else{
if(p.val == q.val){
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
}else
return false;
}
}
}
迭代版:
public class Solution {
public boolean isSameTree(TreeNode p, TreeNode q) {
Queue<TreeNode> queueP = new LinkedList<TreeNode>();
Queue<TreeNode> queueQ = new LinkedList<TreeNode>();
TreeNode p1, p2;
queueP.add(p);
queueQ.add(q);
while(!queueP.isEmpty() && !queueQ.isEmpty()){
p1 = queueP.poll();
p2 = queueQ.poll();
if(p1 == null && p2 != null ||
p1 != null && p2 == null)
return false;
if(p1 == null && p2 == null)
continue;
if(p1.val == p2.val){
queueP.add(p1.left);
queueP.add(p1.right);
queueQ.add(p2.left);
queueQ.add(p2.right);
}else return false;
}
if(queueP.isEmpty() && queueQ.isEmpty())
return true;
return false;
}
}
LeetCode OJ:Same Tree(相同的树)的更多相关文章
- LeetCode OJ Symmetric Tree 判断是否为对称树(AC代码)
思路: 主要判断左子树与右子树. 在判断左时,循环下去肯定会到达叶子结点中最左边的结点与最右边的结点比较. 到了这一步因为他们都没有左(右)子树了,所以得开始判断这两个结点的右(左)子树了. 当某 ...
- LeetCode OJ——Binary Tree Inorder Traversal
http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 树的中序遍历,递归方法,和非递归方法. /** * Definition ...
- [LeetCode] Graph Valid Tree 图验证树
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 【LeetCode】Same Tree(相同的树)
这道题是LeetCode里的第100道题. 这是题目: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 ...
- [LeetCode OJ] 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 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [leetcode]100. Same Tree相同的树
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- LeetCode 100. Same Tree相同的树 (C++)
题目: Given two binary trees, write a function to check if they are the same or not. Two binary trees ...
- LeetCode 101. Symmetric Tree(镜像树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- github的代码上传成功但是不显示绿格子(一直拖....心痛的教训.....)
损失了我特么的很多格子啊啊啊啊,必死强迫症啊!!!! 究其原因就是客户端绑定邮箱错误 本地 :git config user.email 显示邮箱是否和github中设定的一样? git config ...
- WCF 远程服务器返回了意外响应: (400) Bad Request。
WCF 端 <system.web> <httpRuntime maxRequestLength="2147483647" /> </system. ...
- 1.1 使用电脑测试MC20模块的基础使用和测试
需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...
- gearman background后台job状态获取
GearmanClient background job有一个方法叫: public array GearmanClient::jobStatus ( string $job_handle ) Get ...
- input propertychange(1)
input type=“text” 通过js改变输入框的value值是不会出发input propertychange事件
- iOS 系统认知 debug distribution release 和 #ifdef DEBUG
debug:调试模式 有调试信息 线下 release: 无调试信息 经过了编译优化 发布 给用户使用的 线上模式 一般 工程项目 都是自带 上述两种配置结构 还有出现 distribution: ...
- Linux用户和用户组管理 用户组管理命令
添加用户组命令:groupadd 命令格式: [root@localhost ~]# groupadd [选项] 组名 选项: 选项 选项说明 -g GID 指定组ID: 修改用户组命令:groupm ...
- Python的装饰器实例用法小结
这篇文章主要介绍了Python装饰器用法,结合实例形式总结分析了Python常用装饰器的概念.功能.使用方法及相关注意事项 一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让 ...
- Docker 搭建一个Docker应用栈
Docker应用栈结构图 Build Django容器 编写docker-file FROM django RUN pip install redis build django-with-redis ...
- apache基于端口的虚拟主机配置
主机ip: 192.168.7.51 Centos6.5 三个目录/usr/ftp/test/usr/ftp/dev/usr/ftp/demo 实现效果192.168.7.51:8052访问/usr/ ...