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 ...
随机推荐
- centos7 Mysql5.6 升级Mysql5.7
1 2. 卸载Mysql5.6 ,一共有三个包 要卸载: (1)先卸载mysql-server包 : 执行命令 yum remove mysql mysql-server (2)再卸载mysql-c ...
- Oracle 11G无法导出空表的解决办法
11G中有个新特性,当表无数据时,不分配segment,以节省空间解决方法:1.insert一行,再rollback就产生segment了.该方法是在在空表中插入数据,再删除,则产生segment.导 ...
- 和openjdk在一起的第一天-第一次接触有些生疏,就先熟悉一下吧
暂时搞到了openjdk7,就将就着看吧,(为什么csdn全部都是收费啊,llllj),但是还是决定先读这个openjdk7 还有,不知道怎么的就想试试markdown,也不知道好用不 hello w ...
- 随心所欲移动Panel
C# Winform编程时,有时需要在程序执行时,使窗体中的panel控件可以随意的移动,这时可以采用下面这种方法: 主要包括以下两步: @1:给panel(此处以 RealGLPanel为例说明)添 ...
- Android零散知识点积累
本文仅在记录android开发中遇到的零散知识点,会不断更新... 目录 .隐藏系统标题栏 .图片尺寸及屏幕密度 3.获取顶部状态栏高度 1.隐藏系统标题栏 1)在资源文件styles.xml中定义样 ...
- Oracle处理Clob类型数据入库(String入库)
从网上查找一堆参考,要么语焉不详,要么不可行.自己鼓捣了一堆可以正常入库了.请看最后: insert into CP_V_INFO" + "(ID, "+ "P ...
- ZOJ 3961 Let's Chat 【水】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3961 题意 给出两个人的发消息的记录,然后 如果有两人在连续M天 ...
- FTP下载
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import ja ...
- Qt5.3.0的安装与测试
Qt5.3.0的安装与测试(交叉编译,用于arm,支持tslib触摸屏) 本次移植可以使用触摸屏. 首先下载源码包: http://download.qt.io/official_releases/q ...
- 【转载】格式化存储装置成为 Ext2/Ext3/Ext4 档案系统
格式化 用系统管理员帐户 (即 root) 身份打「mkfs -t ext2|ext3|ext4 储存装置」: mkfs -t ext3 /dev/sdb5 要格式化档案系统为 Ext2,亦可以直接使 ...