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 ...
随机推荐
- WSL学习:安装ArchLinux和Root/Cling以及注意事项
1.前言 在Windows下使用Linux常见的软件存在许多困难.选择之一是使用VMWare和VirtualBox之类的虚拟机软件,另一种选择是使用类UNIX模拟环境,如Cygwin,GNUWin32 ...
- Jumpserver0.5使用说明
1.系统设置 a.基本设置,这里的ip是jumpserver所在的地址 b.邮件设置,得在qq邮箱中启用授权码 可参考:https://service.mail.qq.com/cgi-bin/help ...
- Codeforces 734C [水][暴力][贪心]
题意: 要生产n个物品,每个花费时间为x. 有两种魔法,每种最多使用1个. 其中第一种魔法可以使每个物品生产的花费时间变为ai,相应的花费是bi;第二种魔法可以减少ci个物品,相应的花费是di,并且保 ...
- Ruby 定时任务之一(初步尝试)
最近工作需要用到定时任务.原来写java的时候也用到过类似的Scheduler的功能. Ruby语言中也有同样功能的工具.rufus-scheduler.下面介绍一下rufus-scheduler. ...
- Jenkins集成java非maven/ant项目的打包思路
打包的思路如下: 1.使用javac命令对代码进行编译,比如递归编译整个项目的java代码.(注意:需要一一对应引用的jar包) 2.输出并整理war包的文件夹结构目录,参考:http://www.c ...
- mysql-实现远程连接(授权法)
远程连接阿里云主机的mysql,遇到以下问题: 1.连接被拒,无法连接 可能原因:1.3306(默认)端口未开放,在控制台设置防火墙规则: 2. host字段的值改为%就表示在任何客户端机器上能以ro ...
- centos 7 关闭firewalld开启iptables
1: 关闭系统高级防火墙firewalld systemctl stop firewalld.service #停止firewall systemctl disable firewalld.servi ...
- CSS3的box-shadow
基础说明: 外阴影:box-shadow: X轴 Y轴 Rpx color; 属性说明(顺序依次对应): 阴影的X轴(可以使用负值) 阴影的Y轴(可以使用负值) 阴影 ...
- 黑苹果+win10双系统折腾笔记
寒假趁机在家折腾一下黑苹果 笔记本配置:神船K610D I7 4600 ,其他配置思路一样,驱动要自己找 镜像和工具:OS X Yosemite 10.10.3 镜像 WIN10 TLSB 2016 ...
- 2017.2.9 开涛shiro教程-第十章-会话管理(一)
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十章 会话管理(一) 10.1 会话 shiro提供的会话可以用 ...