LeetCode(100)题解--Same Tree
https://leetcode.com/problems/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.
思路: DFS
AC代码:
1.递归
/**
* 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) {
if (p==NULL && q==NULL)
return true;
else if(p!=NULL&&q!=NULL){
bool ju;
if(p->val==q->val)
ju=true;
else
return false;
if (p->left==NULL && q->left==NULL)
;
else if(p->left!=NULL && q->left!=NULL)
ju=ju&&isSameTree(p->left,q->left);
else
return false;
if (p->right==NULL && q->right==NULL)
;
else if(p->right!=NULL && q->right!=NULL)
ju=ju&&isSameTree(p->right,q->right);
else
return false;
return ju;
}
else
return false;
}
};
2.非递归
/**
* 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) {
if(p==NULL&&q==NULL)
return true;
else if(p!=NULL && q!=NULL){
stack<TreeNode*> ms1,ms2;
ms1.push(p);
ms2.push(q);
TreeNode* p1,*q1;
while(!ms1.empty()){
p1=ms1.top();
q1=ms2.top();
if(p1->val!=q1->val)
return false;
else{
ms1.pop();
ms2.pop();
if(p1->right==NULL&&q1->right==NULL)
;
else if(p1->right!=NULL&&q1->right!=NULL){
ms1.push(p1->right);
ms2.push(q1->right);
}
else
return false;
if(p1->left==NULL&&q1->left==NULL)
;
else if(p1->left!=NULL&&q1->left!=NULL){
ms1.push(p1->left);
ms2.push(q1->left);
}
else
return false;
}
}
return true;
}
else
return false;
}
};
LeetCode(100)题解--Same Tree的更多相关文章
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [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 & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
- LeetCode 971. Flip Binary Tree To Match Preorder Traversal
原题链接在这里:https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/ 题目: Given a bina ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- Leetcode 简略题解 - 共567题
Leetcode 简略题解 - 共567题 写在开头:我作为一个老实人,一向非常反感骗赞.收智商税两种行为.前几天看到不止两三位用户说自己辛苦写了干货,结果收藏数是点赞数的三倍有余,感觉自己的 ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
随机推荐
- no update
cd /home/.gnupg/ mv gpg.conf gpgbake.conf pacman -S archlinux-keyring && pacman -Syu 如果还是不行, ...
- 转载自——Json.net动态序列化以及对时间格式的处理
关于我工作中对Json处理的东西 第一:动态序列化类 第二:时间格式处理 通常我们一个类里 可能有十到更多的属性,但是我们序列化通常只需要序列化其中的 三到五个这样的话就会有多余的数据 如果 我只想序 ...
- AC日记——队列安排 洛谷 P1160
队列安排 思路: 链表裸题: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> usi ...
- AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369
[模板]普通平衡树(Treap/SBT) 思路: 劳资敲了一个多星期: 劳资终于a了: 劳资一直不a是因为一个小错误: 劳资最后看的模板: 劳资现在很愤怒: 劳资不想谈思路!!! 来,上代码: #in ...
- (4)ASP.NET内置对象1
一.Response 把数据从服务端发送到客户端 Response.Write() 在页面上输出数据 Response.WriteFile(@"F:\WriteFile.txt") ...
- Unity工程资源破解
Unity工程资源提取其实还是很方便的,网上也有很多相关介绍,比如雨凇就专门写了一遍关于破解Unity资源的文章(http://www.xuanyusong.com/archives/3618 ...
- Hadoop OutputFormat浅析
问题:reduce输出时,如果不是推测任务写结果时会先写临时目录最后移动到输出目录吗? 下面部分转自Hadoop官网说明 OutputFormat 描述Map/Reduce作业的输出样式. Map/R ...
- centos7下使用wget命令安装mysql
1.首先安装wget命令: yum -y install wget 2.下载mysql wget http://repo.mysql.com/mysql-community-release-el7- ...
- SecureCRT发送键盘按键对应表(转义字符)
\r 发送回车(CR) \n 发送换行符(LF) \b 发送退格 \e 发送一个转义 \t 发送一个标签 \\ 发送一个反斜杠字符 \v 将剪贴板的内容粘贴到活动状态会话窗口 \p 暂停一秒钟
- Debugging that latch timeout
https://troubleshootingsql.com/tag/stack-dump/ Book on Azure and SQL Server