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 ...
随机推荐
- spring task定时器的配置使用
spring task的配置方式有两种:配置文件配置和注解配置. 1.配置文件配置 在applicationContext.xml中增加spring task的命名空间: xmlns:task=&qu ...
- ABAP开发中message dump
系统里边 消息 造成dump示例, 1.面向对象的method 中一般不能用stop, 例如data_change事件, ** sm30 不能stop, 2. 增强中 有些地方不能stop, 3.还有 ...
- C#编写图书列表winform
Book.cs文件 using System; using System.Collections.Generic; using System.Linq; using System.Text; usin ...
- Android:日常学习笔记(2)——分析第一个Android应用程序
Android:日常学习笔记(2)——分析第一个Android应用程序 Android项目结构 整体目录结构分析 说明: 除了APP目录外,其他目录都是自动生成的.APP目录的下的内容才是我们的工作重 ...
- KVM虚拟化虚拟机支持虚拟化
一.开启的时候需要关闭所有虚拟机: 首先检查 KVM host(宿主机/母机)上的kvm_intel模块是否打开了嵌套虚拟机功能(默认是开启的): 1.modinfo kvm_intel | grep ...
- 教你在Yii2.0框架中如何创建自定义小部件
本教程将帮助您创建自己的自定义小部件在 yii framework 2.0.部件是可重用的模块和用于视图. 创建一个小部件,需要继承 yii\base\Widget,覆盖重写 yii\base\Wid ...
- 玩转python主题模型程序库gensim
gensim是python下一个极易上手的主题模型程序库(topic model),网址在:http://radimrehurek.com/gensim/index.html 安装过程较为繁琐,参考h ...
- 【HackerRank】Maximizing XOR
给定两个整数:L 和 R ∀ L ≤ A ≤ B ≤ R, 找出 A xor B 的最大值. 输入格式 第一行包含 L 第一行包含 R 数据范围 1 ≤ L ≤ R ≤ 103 输出格式 输出最大的异 ...
- 超酷Loading进度条
在线演示 本地下载
- 20145231《Java程序设计》第四次实验报告
实验四 Android开发基础 实验内容 •安装Android Studio •运行安卓AVD模拟器 •使用Android运行出模拟手机并显示自己的学号 实验步骤 一.安装Android Studio ...