【一天一道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 ...
随机推荐
- 数组查找算法的C语言 实现-----线性查找和二分查找
线性查找 Linear Search 用户输入学生学号的成绩 二分查找 Binary Search 要求数据表是已经排好序的 程序存在小的瑕疵
- 浅谈Log4net在项目中如何记录日志
一 引入背景 在软件开发周期中,无论是开发中,或是测试中,或是上线后,选择合适的工具监控程序的运行状态至关重要,只有如此,才能更好地排查程序问题和检测程序性能问题等.本篇文章主要与大家分享,如何 ...
- 微信小程序 发现之旅(一)—— 项目搭建与页面跳转
开发微信小程序需要注册一个小程序账号,具体流程可以参照官方教程: https://mp.weixin.qq.com/debug/wxadoc/dev/index.html 开通账户之后,在 “开发设置 ...
- C++框架_之Qt的开始部分_概述_安装_创建项目_快捷键等一系列注意细节
C++框架_之Qt的开始部分_概述_安装_创建项目_快捷键等一系列注意细节 1.Qt概述 1.1 什么是Qt Qt是一个跨平台的C++图形用户界面应用程序框架.它为应用程序开发者提供建立艺术级图形界面 ...
- MongoDB Limit与Skip方法
MongoDB Limit() 方法 如果你需要在MongoDB中读取指定数量的数据记录,可以使用MongoDB的Limit方法,limit()方法接受一个数字参数,该参数指定从MongoDB中读取的 ...
- Hibernate使用自定义脚本替换注解或者xml文件中的自动生成表结构
本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50534361 我们都清楚,可以使用hibernate的metada ...
- Programming In Scala笔记-第六章、函数式对象
这一章主要是以定义和完善一个有理数类Rational为线索,分析和介绍有关类定义,构造函数,方法重写,变量定义和私有化,以及对操作符的定义等. 一.Rational类定义和构造函数 1.定义一个空类 ...
- [Android]聊聊ActionMode
最近一段时间都没有更新文章,趁工作之余,更新一篇. 今天介绍一个很常见效果也最容易被忽略的弹出框:ActionMode.主要是ActionMode使用和自己使用过程中遇到的一些问题,相对还是比较简单的 ...
- Android中GridView的一些特殊属性
GridView的一些特殊属性: 1.android:numColumns="auto_fit" //GridView的列数设置为自动 2.android:columnWidt ...
- 关于在页面中针对不同版本的IE浏览器实现不同的JS或者CSS样式
一般会用到<!--[if IE]>这里是正常的html代码<![endif]--> 条件注释只能在windows Internet Explorer(以下简称IE)下使用,因此 ...