【一天一道LeetCode】#100. Same Tree(100题大关)
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
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.
(二)解题
题目大意:比较两个二叉树是否相等
解题思路:采用深度优先搜索,依次遍历两个树,判断每个节点是否相等。
博主利用栈实现非递归的二叉输深度优先搜索,并判断每个节点
/**
* 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) {
stack<pair<TreeNode*,TreeNode*>> TwoTreeNode;//用栈实现非递归
TwoTreeNode.push(make_pair<TreeNode*,TreeNode*>((TreeNode*)p,(TreeNode*)q));//初始化
while(!TwoTreeNode.empty())
{
auto temp = TwoTreeNode.top();//去除栈顶
TwoTreeNode.pop();//处理当前节点
TreeNode* ptemp = temp.first;
TreeNode* qtemp = temp.second;
if(ptemp==NULL&&qtemp==NULL) continue;//两个都为空
else if(ptemp!=NULL&&qtemp!=NULL){//两个都不为空
if(ptemp->val==qtemp->val)//判断值是否相等
{
TwoTreeNode.push(make_pair(ptemp->left,qtemp->left));//相等则放入栈等待处理
TwoTreeNode.push(make_pair(ptemp->right,qtemp->right));
}
else return false;//不相等返回false
}
else return false;//一个为空另一个不为空直接返回false
}
return true;//全部处理完都相等就返回true
}
};
【一天一道LeetCode】#100. Same Tree(100题大关)的更多相关文章
- 100.Same Tree(E)
100. same tree 100. Same Tree Given two binary trees, write a function to check if they are the same ...
- <LeetCode OJ> 100. Same Tree
100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy Given two binary tr ...
- LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number
100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; ...
- LeetCode 100. Same Tree (判断树是否完全相同)
100. Same Tree Given two binary trees, write a function to check if they are the same or not. Two bi ...
- leetcode 100. Same Tree、101. Symmetric Tree
100. Same Tree class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL &am ...
- LeetCode解题录-51~100
[leetcode]51. N-QueensN皇后 Backtracking Hard [leetcode]52. N-Queens II N皇后 Backtracking Hard [leet ...
- LeetCode高频题目(100)汇总-Java实现
LeetCode高频题目(100)汇总-Java实现 LeetCode高频题目(100)汇总-Java实现 目录 第01-50题 [Leetcode-easy-1] Two Sum [Le ...
- 100. Same Tree(C++)
100. Same Tree Given two binary trees, write a function to check if they are equal or not. Two binar ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- MockHttpServletRequestBuilder中content和param的区别
结论: Mock将URL的参数和通过使用param添加的参数添加到request中的parameter中(url参数) 而将content内容.类型并没有进行解析,直接添加到request的conte ...
- python常用执行方式&变量&input函数
linux系统中执行py文件方式: ./a.py 需要执行权限 chmod -R 777(最大权限) 常用执行方式: 1. ./a.py2. python a.py 文件内部头加上 #!/usr/b ...
- Git-gitblit-Tortoisegit 搭建Windows Git本地服务器
1.Gitblit安装 1.1.Gitblit简介 Git在版本控制领域可谓是深受程序员喜爱.对于开源的项目,可以免费托管到GitHub上面,相当的方便.但是私有项目托管到GitHub会收取相当昂贵的 ...
- Node.js Smalloc
稳定性: 1 - 试验 类: smalloc 由简单内存分配器(处理扩展原始内存的分配)支持的缓存.Smalloc 有以下函数: smalloc.alloc(length[, receiver][, ...
- Linux 虚存的性能问题
虚存子系统是所有 UNIX 系统的核心组件.下面讨论虚存系统的实现及其对操作系统中几乎其他所有子系统的作用和影响.首先详细说明一些基本的内存管理问题:然后具体分析 Linux 操作系统如何实施虚存管理 ...
- Java程序员的Golang入门指南(上)
Java程序员的Golang入门指南 1.序言 Golang作为一门出身名门望族的编程语言新星,像豆瓣的Redis平台Codis.类Evernote的云笔记leanote等. 1.1 为什么要学习 如 ...
- 判断&数学&生活
作者:黄永刚 初次接触<概率论与数理统计>这门课的时候,脑袋中只有三个词:黑球.白球.袋子,所有的课程内容就是先取,后取,接触一月之后成功的被放趴下了,因此对于这门课程是没有什么好感的,考 ...
- JSP基础概要
[版权申明:本文系作者原创,转载请注明出处] 文章出处:http://blog.csdn.net/sdksdk0/article/details/51925232 作者:朱培 ID:sdksdk0 一 ...
- 在Mac上搭建React Native开发环境
概述 前面我们介绍过在window环境下开发React Native项目,今天说说怎么在mac上搭建一个RN的开发环境. 配置mac开发环境 基本环境安装 1.先安装Homebrew:用于安装Node ...
- 微信小程序基础之新建的项目文件图解
昨天发布的文章,感觉对于学习不够直观,所以今天重点在图标上进行了详细的对应介绍,稍后会尝试开发小程序控件的使用.转载请标注出处,谢谢!