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 ...
随机推荐
- 004-ibus输入法,快捷键,浏览器
一.输入法 用 root 身份在终端下,运行下面命令: yum install ibus-pinyin ibus ibus-gtk ibus-qt 使用im-chooser命令,选择ibus为默认输入 ...
- KVM虚拟化安装配置
一.KVM的基础配置及安装: 1.查看是CPU否支持虚拟化: [root@oldboy-node1 ~]# grep -E "(vmx|svm)" /proc/cpuinfo vm ...
- cocos2d关于glew32.lib错误(转)
应项目需要使用cocos2d-x开发,又要学习新东东了.·cocos2d-x 是一个支持多平台的 2D 手机游戏引擎,用C++重写cocos2d-iphone引擎的一个开源项目,想了解更多的童鞋美去百 ...
- 树莓派打造对话机器人 Python(转)
工具列表 1. **树莓派**(型号不要求,本人使用的是3B) 2. **usb麦克风**(某宝有卖,我就不打广告了) 用来录音 3. **音响或者喇叭**(某宝也有卖) 用来播放 以上就是需要的工具 ...
- Docker容器技术-第一个容器
一.第一个容器 1.Docker版本 A.community-edition社区版 Docker CE是免费的Docker产品的新名称,Docker CE包含了完整的Docker平台,非常适合开发人员 ...
- 20145231第二周Java学习笔记
20145231 <Java程序设计>第2周学习总结 教材学习内容总结 本周的学习采用的依然是先看课本,再看视频,然后实践敲代码,最后根据学习笔记总结完成博客. 第三章:基础语法 知识点比 ...
- windows7下手工搭建Apache2.2 php5.3 Mysql5.5开发环境
Apache2.2(apache_2.2.2-win32-x86-no_ssl)php5.3.5(php-5.3.5-Win32-VC6-x86,请注意选择VC6版本,否则无法加载php5apache ...
- INSPIRED启示录 读书笔记 - 第29章 大公司如何创新
大公司实现创新的方法 20%法则:谷歌的程序员有20%的工作时间可以用来从事创新研究,这个方法最早是从施乐帕克研究所学来的.20%法则鼓励普通员工自己尝试各种想法,让员工打心底愿意倾注更多的激情和汗水 ...
- SQL Server 利用WITH AS递归获取层级关系数据
WITH AS短语,也叫做子查询部分(subquery factoring),在SQL Server 2005中提供了一种解决方案,这就是公用表表达式(CTE),使用CTE,可以使SQL语句的可维护性 ...
- Python 字符串概念和操作
# 字符串概念:由单个字符串组成的一个集合 # 普通字符串(非原始字符串) str = "abc" print(str) # abc # 原始字符串(前面加r) str = r&q ...