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 ...
随机推荐
- C语言编程中函数指针的定义及使用
C语言中函数指针的定义: typedef int (*funcPtr)(int, int)表示定义了一个函数指针funcPtr,这个函数指针只能指向如下: int add(int, int).int ...
- linux根文件系统制作之busybox编译和系统构建【转】
转自:http://blog.chinaunix.net/uid-29401328-id-5019660.html 介绍完相关文件后我们开始构建文件系统,涉及到的文件等到具体用到的时候再讲. 一.编译 ...
- web项目中引入jquery easyui
jQuery easyui是一个基于jquery的用户界面插件集合,可以做出各种炫酷页面效果,大中型项目都可以使用些框架,非常好用,而且它有中文网,提供了大量的demo.下面我们看怎么将它引入到项目中 ...
- 如何配置tomcat环境变量
首先下载tomcat,并且解压到目录: 注意:2,3步的变量值要到下图这一步 即,bin的上一级目录不包含bin 1.第一步鼠标右键计算机->属性->高级系统设置,进去之后,点击环境变量, ...
- HDU 5997 rausen loves cakes(启发式合并 + 树状数组统计答案)
题目链接 rausen loves cakes 题意 给出一个序列和若干次修改和查询.修改为把序列中所有颜色为$x$的修改为$y$, 查询为询问当前$[x, y]$对应的区间中有多少连续颜色段. ...
- workflow engine Ruote初体验之一(概念)
由于最近自己写点小东西,需要有工作流程管理方面的应用,所有的环境为Ruby on rails,所有在选择流程引擎的时候选择了ruote,但是对于ruote是完全陌生的,所以在这里记下点滴,如果理解的不 ...
- Linux下设置开机启动
新配置了vsftpd 需要设置ftp开机启动,linux新手,还不是很熟悉linux下的操作! 查询后发现命令是: chkconfig vsftpd on chkconfig命令用于设置运行级别 ...
- Android OkHttp经验小结
OkHttp应该是目前最完善,也是相当流行的一个底层网络请求库.Google都在用,所以有必要深入了解一下,刚好最近在重构公司项目的网络层,就顺便梳理一下.———–12.29————最近暂时没有时间详 ...
- 转: JDK包含的基本组件
JDK(Java Development Kit)是Sun Microsystems针对Java开发员的产品.自从Java推出以来,JDK已经成为使用最广泛的Java SDK.JDK 是整个Java的 ...
- spring boot 读取配置文件(application.yml)中的属性值
在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值: 1.引入依赖: <!-- 支持 @ConfigurationProperties 注 ...