【一天一道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 ...
随机推荐
- Lintcode389 Valid Sudoku solution 题解
[题目描述] Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where empty ...
- 关于html+ashx开发中几个问题的解决方法的感想和总结
1.针对上篇文章中的服务端处理不敢苟同.仍然坚持使用反射,建立BaseHandler.ashx并在默认process方法中写上反射方法以及权限验证方法.针对具体的情况返回对应的值.服务端其他handl ...
- PHP MySQL Where 子句
WHERE 子句 WHERE 子句用于提取满足指定标准的的记录. 语法 SELECT column_name(s) FROM table_name WHERE column_name operator ...
- Python3 基础语法
编码 默认情况下,Python 3源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: cp-1252 -*- 标 ...
- Java Web前端到后台常用框架介绍
一.SpringMVC http://blog.csdn.net/evankaka/article/details/45501811 Spring Web MVC是一种基于Java的实现了Web MV ...
- Scala:数组
http://blog.csdn.net/pipisorry/article/details/52902432 ) 或var z = new Array[String](3) 以上语法中,z 声明一个 ...
- 剑指Offer——“你最大的缺点是什么”回答技巧及范例
剑指Offer--"你最大的缺点是什么"回答技巧及范例 问题分析:认识自己的缺点是一个巨大的优点, 当HR问到你缺点的时候, 你的机会来了, 请快展示你的自知之明吧!你想把优点 ...
- 28 自定义View画坐标和柱状图
自定义View类 RectView.java package com.qf.sxy.day29_customview.widget; import android.content.Context; i ...
- 24 AIDL案例
服务端 MainActivity.java package com.qf.day24_aidl_wordserver; import android.app.Activity; import andr ...
- Swift中使用NSLog的问题
在Swift中如果想要以如下方式执行NSLog,则编译器必定抱怨: class Foo {} NSLog("%@",Foo()) 因为Foo是原生Swift类不是继承自NSObje ...